Java Basic Problems

Java: How to show image in JLabel

How to show image on JLabel:

If we need to show image as a background of a JLabel, then we can easily show it by using ImageIcon and then by setting that  ImageIcon to the JLabel.

Download this project from:

http://www.ziddu.com/download/12618536/jLabelImgIcon.zip.html

I have uploaded the project  and I used  NetBeans IDE 5.0 (You can use latest version) and JDK 1.4.2

I am not giving the whole code here, just I am mentioning the Core Portion of the code. Better you download the project and Run it in your computer.

Code:

import javax.swing.*;

import java.awt.image.BufferedImage;

public class jLabelImgIcon extends javax.swing.JFrame {

private void myInit()

{

 ImageIcon icon = new ImageIcon(“C:\\myworkspace\\sample.png”);

 icon = new ImageIcon(icon.getImage().getScaledInstance(100, 100, BufferedImage.SCALE_SMOOTH));

 jLabel1.setIcon(icon);     

    }

——————

—————

}

 OutPut:

In the output Image is viewing on JLabel  using ImageIcon and jLabel1.setIcon.

JLabel Image Icon

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s