Load Image from Path to JLabel in Java


Image is one of the file formats that can help your designs more attractive. There are many types of image that we can use in Web or Desktop applications, the jpeg, gif, png, and bitmap extensions.

This tutorial entitled “Load Image from Path to JLabel in Java” will help you to understand on how to load an image from path to a jLabel element in Java. This tutorial will use jLabel, jTextField, and jButton to complete this tutorial.

Load Image Path JLabel Using Java Steps

  1. Create/Add a new jFrame Form in your project

2. Design your jFrame form just look like the image below.

3. Add the following imports above your class to access the library for Image, and Icon.

  1. import java.awt.Image;
  2. import javax.swing.ImageIcon;

4. Double click your button and add the following codes below

  1. ImageIcon imgThisImg = new ImageIcon(new ImageIcon(jTextField1.getText())
  2. .getImage().getScaledInstance(360, 171, Image.SCALE_DEFAULT));
  3. jLabel1.setIcon(imgThisImg);

jTextField1 (Image path)

360 (Image Width)

171 (Image Height)

jLabel1 (Display the Image)

5. Run your program and it should look like the image below.

If you have any questions or suggestion about Load Image Path JLabel using Java, Please feel free to contact us at our contact page.

Facebook Comments


Please enter your comment!
Please enter your name here