0

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?

Noodly_Doodly
  • 69
  • 2
  • 11
  • You should post the **relevant** code here. – BitNinja Nov 12 '14 at 00:28
  • `JLabel@setIcon`? See [How to Use Labels](http://docs.oracle.com/javase/tutorial/uiswing/components/label.html) for more details – MadProgrammer Nov 12 '14 at 00:31
  • @MadProgrammer I've already read those tutorials. Now that I have a set ImageIcon, how can I change it while the app is running? – Noodly_Doodly Nov 12 '14 at 00:33
  • 1- `JFrame` doesn't have a `paintComponent` method, so that's not going to be called anyway; 2- You should always be calling `super.paintXxx` when doing custom painting, but see point 1 for reasons why this won't work... – MadProgrammer Nov 12 '14 at 00:34
  • 1
    I'm sorry, what? You call `setIcon` on the instance of the `JLabel` you want to change when you want to change it... – MadProgrammer Nov 12 '14 at 00:35
  • 1
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. See [Why is it frowned upon to use a null layout in SWING?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) for more details – MadProgrammer Nov 12 '14 at 00:37
  • Don't maintain `static` references to components within GUI's, this suggests a wider series of issues. What happens if you have more than one instance of your `Window` class? – MadProgrammer Nov 12 '14 at 00:40

1 Answers1

0

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.

Kamern
  • 16
  • 3
  • 1- If you need to call `revalidate` and `repaint` after changing the `icon` or `text` of a `JLabel` then there is something wrong with your program, these are bound properties and should trigger updates of their own. 2- Since the OP is using a `null` layout, calling `revalidate` is pointless, as it triggers a change in the layout manager... – MadProgrammer Nov 12 '14 at 00:38
  • Ah you're correct. setIcon shouldn't have this problem. It might be worth flushing instead? – Kamern Nov 12 '14 at 00:42
  • Since the OP hasn't even tried, I doubt there is a problem, you just don't need to make those calls in your example ;) – MadProgrammer Nov 12 '14 at 00:45
  • Thank you, the code works. But, even when I put `charLabel.setIcon(burpIcon);` before `Sounds.burp()` (which plays a burping noise) there is a delay during when the sound is playing. – Noodly_Doodly Nov 12 '14 at 00:57
  • Are you sure your sound file doesn't have silence at the beginning? – Kamern Nov 12 '14 at 00:58
  • More what I meant is that it waits until after it has played the sound to change the icon. And no, it doesn't have sound at the beginning. – Noodly_Doodly Nov 12 '14 at 01:00
  • @Noodly_Doodly: You have to play sounds in a separate thread. Your sound is blocking the GUI update. – Gilbert Le Blanc Dec 20 '14 at 23:10