Static or Dynamic Way to Display Image in jFrame in Java Swing

In this post, SKOTechLearn describe the static and dynamic way to Display Image in jFrame in Java Swing with Netbeans through jLabel. Simply we can add image through Properties Settings or using Code which is described bellow.

Why we use jLabel for displaying image? Because, This is the simple way to handle any files like: jpeg, png etc using jLable pallet.

Open jFrame through Another jFrame in Java Swing

If you want to show external image or use it as internal way like add as logo or banner, then here we will describe as dynamic and static way. So lets learn with following two process:

 (1) Display Image in jLabel in Java Swing Static way 
 (2) Display Image in jLabel in Java Swing Dynamic way 

Come to the point by point description.

(1) Display Image in jLabel in Java Swing Static way :

In this process just create an image file or use your existing file and copy that file in your particular package. For this step by step process let's find following.

Suppose if you create a picture file and want to show. Then first copy that particular file and paste it on your project's package, or you can access through external path.
Drag_Image_File_In_Java_Package
Drag Image File In Package

Tips to Auto Resize Control Setting in Java Netbeans

As you can see, I simply copy my particular file and paste on my package. After that simply drag a jLabel from palette and adjust it's height width and location according to desire and after that follow the steps:


1.
Right click on jLabel.

2.
Select 'Properties' option or go to directly on 'Properties' window.

3.
Find “icon” option. And select your particular file.

4.
If it not show any file just click on 'Custom editor' buttonCustom_Editor_Buton_in_Java

5.
And select file from there.


For 'Custom editor' button, you can see that it will contain with icon's option of Properties windows. For easily understanding process follow the image.
Set_Icon_in_jLabel
jLabel icon setting

When you done all this process. It will display picture like shown in above figure.
So, this is the way of showing picture in static way.

How to Design or Change Look of jLabel in Java in Netbeans


(2) Display Image in jLabel in Java Swing Dynamic way:

If you want to show image in dynamic way, you have to write code for it, Suppose you want to show picture during jFrame's open or activate. So write code as we describe bellow.

But first, you have to import some class inside your java Package:
package simple_java; 

  import java.awt.image.BufferedImage;
  import java.io.IOException;
  import javax.imageio.ImageIO;
  import javax.swing.ImageIcon;

Now lets write show picture code on WindowsOpened evets:
private void formWindowOpened(java.awt.event.WindowEvent evt) { 
   try{
       BufferedImage image1 = ImageIO.read(getClass().getResource("skotechlearn.png"));
       jLabel1.setIcon(new ImageIcon(image1));
   }catch(IOException imgl){}

 }   

How to show external image in jLabel?

If you want to show picture from external url or path, then write code like:
String myimgpath; 
myimgpath= "your exteral file path";
Icon myimgicon = new ImageIcon(myimgpath);
jLabel1.setIcon(myimgicon);

When you run application then it will load picture during runtime and display it on jLabel.

Note:- You can also set jLabel as background image by setting the height width of jLabel according to jFrame.

So, this is the way of displaying image on jFrame in java Netbeans with static and dynamic process which is easily described in above given step by step process in SKOTechlearn post.

Simple Tips to Creating Swing GUI Application in Java

For further learning of coding, programming  or simple tips and trick related to OS, follow SKOTechlearn and learn things as simple as possible.

Tips to Use of jList for Showing Data in Java Swing

0 comments: