Reputation: 4047
I have a problem with my script; I want repaint a new image (another one is shown) when a button is pressed, but the button doesn't do anything...
ActionListener one = new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel2.revalidate();
panel2.repaint();
}
};
btn1.addActionListener(one);
JLabel test1 = new JLabel(myDeckOfCards.giveCardPlayer1().getImage());
panel2.add(lab1);
panel2.add(test1);
panel2.add(pn5);
panel2.add(pn1);
panel2.add(btn1);
Upvotes: 0
Views: 2184
Reputation: 10772
Inside actionPerformed
you need to get hold of your JLabel
and call setIcon()
on it, passing in the new image.
There's a few ways to get the JLabel, one is to make sure you have a final
variable declared to contain it somewhere in scope of your actionPerformed
method, and another is to find it from inside the panel2
(not recommended).
You could also pass it in to your ActionListener
through a constructor if you declare a full-fledged class for that purpose.
EDIT:
final JLabel test1 = new JLabel(myDeckOfCards.giveCardPlayer1().getImage());
ActionListener one = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Get 'anotherIcon' from somewhere, presumably from a similar
// place to where you retrieved the initial icon
test1.setIcon(anotherIcon);
}
};
btn1.addActionListener(one);
panel2.add(lab1);
panel2.add(test1);
panel2.add(pn5);
panel2.add(pn1);
panel2.add(btn1);
Upvotes: 5