Java image example: A simple ImageIcon and JLabel technique

Here is some sample Java source code that shows a simple technique to display a GIF, JPG, or PNG image using a Java ImageIcon and JLabel.

In short, with this technique, you create an ImageIcon with the image you want to display, and then add the ImageIcon to your JLabel constructor. After you put the ImageIcon on the JLabel, you can display the JLabel wherever you want, and your image will be displayed.

Here's my Java ImageIcon JLabel example code:

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

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

There are certainly other Java image approaches, and I share many of those on this blog, but I've used this Java image display technique many times.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.