Search code examples
javaswingjlabelimageicon

Completely Changing JLabel on command


What I'm trying to do is have an image set as a label. My problem is when I go to change the image through a confirmation button, nothing happens.

I'm using NetBeans JFrame Designer or whatever, but here is what its doing..

Default, I have the JLabel set as normal no changes or anything. basically I made it and removed the text, so its empty. Now when the user clicks a button, I want an image to come up, as they choose. Basically I'm trying to change the color of an image, that they choose it to go to.

frontDesign = new JLabel(new ImageIcon(Functions.getShirt("front")));

This is what I'm using to change it. basically I'm resetting the JLabel, which isn't doing anything.. Is there another solution, or am I doing something wrong?


Solution

  • Don't create a new label. Changing the reference of a variable does not add the label to the GUI. The newly created label is just sitting in memory doing nothing.

    Just use:

    frontDesign.setIcon(...);
    

    This will replace the icon off the label currently displayed on the frame.