JLabel icon disappears
hello all, im very new to Java and im having a problem with JLabels. I have built a GUI and im using a JLabel with an png icon. what i want to do is when a button is pressed i want this icon to change to another one, im using the following code:
ImageIcon icon = new ImageIcon("product.png");
System.out.println("icon = " + icon); // test for null
when i press the button, the JLabel just disappears. any help would be greatly appreciated.
You may need to post more code.
Originally Posted by Fubarable
But anyway, if what you want is to alternate between two icons everytime a button is pressed, I can think of two alternatives:
1.- Use a JToggleButton, and add an ActionListener to it that checks the state of the button and sets one icon or the other depending on if the button is toggled or not.
2.- Use a normal JButton, and add an ActionListener to it that checks a counter and sets one icon or the other depending on if the value of the counter is odd or even.
And if that's not exactly what you want... Well, the changing of the icon should still happen in the button's ActionListener, so the basic idea is still useful: write an ActionListener that does what you want to do.
thank you very much, i used your button idea and it suits my needs perfectly.