Programmatically change ImageIcon automatically [Java]

465 Views Asked by At

I am making a simple game right now. When a JButton is clicked, the ImageIcon of a JLabel is supposed to change. How can I accomplish this?

1

There are 1 best solutions below

7
On BEST ANSWER

Simple, just quickly run setIcon() on the JLabel via the action listener of the button.

An example:

  if ("burp".equals(evt.getActionCommand())) {
        charLabel.setIcon(burpIcon);
        Sounds.burp();
  }

As mentioned by MadProgrammer, any issue you have seeing real-time changes to setIcon() will probably warrant a look at how you've actually designed the class, rather than attempting a hacky workaround to force the ImageIcon to function as it should with the proper setup.