How to put image in jFrame or Swing application?

            I've got so many requests on my website that how to put image in swing application or Jframe. Its very easy for one to understand. I hope you are using some IDE for creating swing application. I'm using Netbeans here to create one.

To place image on swing application you can use jLabel for that. If you are not using any IDE here is code for you.

private javax.swing.JLabel jLabel1;
jLabel1 = new javax.swing.JLabel();
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon_128.png")));
//Here image placed in package/folder called images. For better explanation read the whole article.

Now lets roll to IDE Netbeans. Netbeans implement same as above i wrote.

Step 1 : Create package/folder in project that contains images for your project.
  • Why is required so? : Say if you are building application for your client that requires images in that. Pointing image out of project folder will not be available in client's computer. So by placing/binding images within project will always available in *.jar file.

Step 2 : Create or place jLable in jFrame. 


Step 3 : Open up jLabel properties and browse icon.

properties jLabel

Step 4 : Select desired image either way from package (Recommended) or external image.

swing, netbeans

Step 5 : Press "Ok" and  now remove text of jLabel.

jLabel

Output : Finally we've done it. You can move your image at your desired place.



0 comments :

Note: only a member of this blog may post a comment.