how-to

Window (swing) applications

How to set image to JLabel

When you need add the image/picture to JLabel use this code. Image could be added from the disc, from resource or you can use some other way - for example image from network.

This example shows reading image from resource = from package in your project.

1. At least - you must copy the image file to your project package.

2. When you have image in same package use this variant:

jLabelPict.setText( "This is text" );
jLabelPict.setIcon( new ImageIcon( this.getClass().getResource( "pict1.png" ) ) );

Or - when you have image in other package, you must specify the "path" to it, and use this variant:

jLabelPict.setText( "This is text" );
jLabelPict.setIcon( new ImageIcon( this.getClass().getResource( "/resource/pict2.png" ) ) );

It shows: