Search code examples
javajlabelimageicon

Loading an ImageIcon onto an existing JLabel


I already know that a JLabel can use an ImageIcon in the declaration of the JLabel, like this.

JLabel stick = new JLabel(new ImageIcon("stickPicture.gif"));

My question is, how to assign the picture to the JLabel after declaration. For example (this isn't actually the code that works, this is just the technique that I thought might have worked)

JLabel stick = new JLabel();
stick = new ImageIcon("stickPicture.gif"));

Kind of an odd question, just wondering if it can be done.


Solution

  • Use JLabel#setIcon(Icon).

    JLabel stick = new JLabel();
    stick.setIcon(new ImageIcon("stickPicture.gif"));