Results 1 to 4 of 4
Thread: JLabel icon disappears
- 05-09-2010, 01:27 AM #1
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
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
picture.setIcon(icon);
when i press the button, the JLabel just disappears. any help would be greatly appreciated.
thanks
L
-
- 05-09-2010, 09:51 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
Agreed.
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.
- 05-09-2010, 12:43 PM #4
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-06-2010, 11:02 PM -
Eclipse appears half a second and disappears Vista IE8
By ortollj in forum EclipseReplies: 11Last Post: 10-27-2009, 07:55 AM -
text box disappears
By okabeer in forum NetBeansReplies: 2Last Post: 07-13-2009, 06:21 PM -
jLabel
By Matty in forum AWT / SwingReplies: 3Last Post: 09-22-2008, 10:22 PM -
JLabel
By Jack in forum AWT / SwingReplies: 2Last Post: 07-02-2007, 01:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks