| Author |
How to add image inside jPanel ??
|
Rite Sara
Ranch Hand
Joined: Feb 13, 2010
Posts: 56
|
|
Hi,
Could you please let me know how to add .png image inside a jPanel ??
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
Use a JLabel with an ImageIcon, or override paintComponent to draw the image (don't forget to call super.paintComponent!)
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Rite Sara
Ranch Hand
Joined: Feb 13, 2010
Posts: 56
|
|
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
Joined: Oct 27, 2005
Posts: 19232
|
|
|
What's not working? Is the image path correct? What LayoutManager are you using? ItDoesntWorkIsUseless. TellTheDetails.
|
 |
Rite Sara
Ranch Hand
Joined: Feb 13, 2010
Posts: 56
|
|
Thanks for helping out Rob ..
I used following approach and its working fine ..
|
 |
Rob Camick
Ranch Hand
Joined: Jun 13, 2009
Posts: 1808
|
|
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.
|
 |
 |
|
|
subject: How to add image inside jPanel ??
|
|
|