Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add image inside jPanel ??

 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Could you please let me know how to add .png image inside a jPanel ??
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a JLabel with an ImageIcon, or override paintComponent to draw the image (don't forget to call super.paintComponent!)
 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried first approach ..

ImageIcon icon = new ImageIcon("C:\\Documents and Settings\\255203\\Desktop\\Image\\New Folder\\final2.png");
JLabel label = new JLabel(icon);
jPanel1.add(label);

But this is not working

Could you please give sum sample code regarding second approach ...



Thanks,
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's not working? Is the image path correct? What LayoutManager are you using? ItDoesntWorkIsUseless. TellTheDetails.
 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for helping out Rob ..
I used following approach and its working fine ..
 
Rob Camick
Ranch Hand
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this is not working


Try reading the JLabel API where you will find a link to the Swing tutorial in "How to Use Labels" which contains a working example.

The tutorial also contains a section on "How to Use Icons" which goes into more detail about how Swing finds the image.

You can also read the section on "Custom Painting" for more help with suggestion 2, but first you should master suggestion 1.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic