Moein Hosseini
Moein Hosseini

Reputation: 4383

Remove and add new JLabel in mouseListener

I have 2 JLabels, and I add MouseListener to one of them,which can remove other JLabel and draw it again(when I click on one of them,the Image of other one change) this is my code,but it does't work,it remove JLabel,but don't paint it again,what should I do?

ImageIcon icon_next = new ImageIcon("next-icon.PNG");
...
next = new JLabel(icon_next);
...
next.addMouseListener(this);
player_img_lbl = new JLabel(player_img_ico[0]);
add(player_img_lbl, FlowLayout.CENTER);
....
@Override
public void mouseClicked(MouseEvent e) {
        this.remove(player_img_lbl);
    JLabel player_img_lbl = new JLabel(player_img_ico[1]);
    add(player_img_lbl0,FlowLayout.CENTER);
    repaint();
}

is there another way exist ,which when I click on one of the JLabel,it can change Image of other one?

Upvotes: 1

Views: 1025

Answers (1)

StanislavL
StanislavL

Reputation: 57381

Call revalidate() before repaint()

Upvotes: 4

Related Questions