Thursday, May 2, 2019

Display or Show static or dynamic Image in jFrame in java

Display or Show static or dynamic Image in jFrame in java

In this post SKOTechLearn describe the way to display or show image in java through jLabel. Why we use jLabel for displaying image? Because, in java this is the simple way to handle any files like: jpeg, png etc.

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:

Come to the point by point description.

 (1) Show image in jLabel with Static way in java:

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

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:
  • Right click on jLabel
  • Select 'Properties' option or go to directly on 'Properties' window.
  • Find “icon” option. And select your particular file.
  • If it not show any file just click on 'Custom editor' buttonCustom_Editor_Buton_in_Java            
  • 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.

 (2) Show image in jLabel with Dynamic way in java:

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:

 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.

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

0 comments: