How to display an image in a JFC/Swing app using an ImageIcon and JLabel

By Alvin J. Alexander, devdaily.com

Here is some sample Java source code showing how to create an ImageIcon (to contain an image, like a JPG or PNG), and then put that ImageIcon on a JLabel. After you put the ImageIcon on the JLabel you can work with the JLabel to show the image wherever you want it displayed.

ImageIcon myImage = ImageIcon(com.devdaily.myapp.MainFrame.class.getResource("openFile.png"));

// Somewhere later in the code ...
JLabel myLabel = new JLabel();
jmyLabel.setIcon(myImage);

devdaily logo