Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to add image inside jPanel ??

 
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 ??
 
Sheriff
Posts: 21758
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
Posts: 21758
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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 ..
 
Rancher
Posts: 3051
22
  • 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
Boost this thread!