• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How to add image inside jPanel ??

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Could you please let me know how to add .png image inside a jPanel ??
 
Sheriff
Posts: 22702
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22702
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for helping out Rob ..
I used following approach and its working fine ..
 
Rancher
Posts: 3287
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic