• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to set a jpeg file on a JLabel?

 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a JLabel & I want to display a jpeg file on it.I tires using ImageIcon ,but it is not displayed.
Source:
*********************
Image img= Toolkit.getDefaultToolkit().getImage("test.jpg");
ImgageIcon ic=new ImageIcon(img);
label.setIcon(ic);
*******************
what is wrong in this code?Please help me to display a jpeg image file on a JLabel.
Happy middling with java.
Netharam.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep... that's the way to add an image to a JLabel... try to print out the size of the image using


If it is 0 x 0, or you get a NullPointerException, it is having a problem finding the image... either you need to specify the path to the image, or the image's filename is misspelled.

-Nate
 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Nate the image file is ok,but it doesn't get displayed.Is the way to dislplay images is right as what I have done?
The same works in the case of "gif" images.What is the problem.I've checked with the path & also the size of the file,but it works in the case of gifs & not jpegs.
Please do reply this Nate.
Happy middling with java.
Netharam.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does the label get shown?
add some text (string) to the label to see if the problem is there.
also check that the jpg file is in the current directory of the class file....
 
meera sood
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
try adding your image to a mediatracker after instantiating it using the toolkit, and i think it should work..
hope this helps...
meera
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have You tried createImage() instead?
Maybe this will work.
Regards
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!