GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes How do I display an image on a JLabel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I display an image on a JLabel" Watch "How do I display an image on a JLabel" New topic
Author

How do I display an image on a JLabel

Sowjanya Raghu
Greenhorn

Joined: Oct 29, 2000
Posts: 17
I am having trouble trying to display an image on a JLabel.
The code I have been trying is as follows:
ImageIcon icon = new ImageIcon ("airplane4.gif");
System.out.println("Size of the Image = " +
icon.getIconHeight() + "..." +
icon.getIconWidth());
if (icon == null) {
System.out.println("Icon not loaded");
} else {
System.out.println("Icon loaded = " + icon);
}
JLabel imageLabel = new JLabel(icon);
When I try to display the loaded images's width and height, I get -1 as the reply + The image is also not displayed.
Any idea as to where I am going wrong will be much appreciated.
Thanks,
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
It looks like your image is not loaded. Are you sure that the path to the image is in your classpath?
W.
Sowjanya Raghu
Greenhorn

Joined: Oct 29, 2000
Posts: 17
The image is definitely getting loaded 'cause when I run the code "Icon loaded = " + icon is displayed.
But the return values of getIconHeight() and getIconWidth() are
-1.
I wonder if I have to do something else.
Thanks for the reply,
S
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Your icon is not getting loaded. I just used your code with a non-existent image and it followed the loaded path. Changed it to a valid one and it works fine. The way you have the image loaded it must exist in the same directory as the class file that is running. Check where your image is and where your class file is and change
ImageIcon icon = new ImageIcon ("airplane4.gif");
to match this.
 
GeeCON Prague 2014
 
subject: How do I display an image on a JLabel