Mohammed Essam
Mohammed Essam

Reputation: 3

How can I add JLabel with background image in loop

I need to make room JLabel with background. This background it's state of room just I want way to change this colors to images in loop I make a hotel booking room and some other service and I don't know how to do this

private void checkstatus() {
    jPanel2.removeAll();
    int dx = 1; 
    ResultSet Rroom;
    Rroom = DB.RunQuery("Select * From rooms");

    final ImageIcon imageIcon = new ImageIcon("c://Des.jpg");

    try {
        while(Rroom.next()) {
            String Rstatus = Rroom.getString(3);
            rou+=40;

            Label xlabel;
            xlabel = new Label("Label 1",Label.CENTER);
            Font bigFont = new Font("SanSerif", Font.BOLD, 11);
            xlabel.setFont(bigFont);

            if(Rstatus.equals("Busy"))
                xlabel.setBackground(Color.red);
            else if (Rstatus.equals("Test"))
                xlabel.setBackground(Color.CYAN);
            else
                xlabel.setBackground(Color.green);

            jPanel2.add(xlabel);
            xlabel.setForeground(Color.black);
            xlabel.setBounds(new Rectangle(rou, rou2, 35, 35));
            xlabel.setText("1");

            if(rou==365) {
                rou=-35;
                // El 2rtfa3
                rou2 +=40;
            }

            dx++; 
        }  // end of loop

        rou = -35; // for new check
        rou2=10; // for new check   

    }// try 
    catch (SQLException e) {
    }
}

Upvotes: 0

Views: 1112

Answers (2)

FThompson
FThompson

Reputation: 28687

To display a JLabel with an icon, you should use JLabel#setIcon(Icon).

xlabel.setIcon(new ImageIcon(yourImage));

Upvotes: 1

evg
evg

Reputation: 1338

You can use

setIcon(Icon icon)

method of JLabel class.

Upvotes: 1

Related Questions