Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You may need to post more code.
    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.

  4. #4
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    thank you very much, i used your button idea and it suits my needs perfectly.

    L

Similar Threads

  1. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-07-2010, 12:02 AM
  2. Replies: 11
    Last Post: 10-27-2009, 08:55 AM
  3. text box disappears
    By okabeer in forum NetBeans
    Replies: 2
    Last Post: 07-13-2009, 07:21 PM
  4. jLabel
    By Matty in forum AWT / Swing
    Replies: 3
    Last Post: 09-22-2008, 11:22 PM
  5. JLabel
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 02:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •