aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to add image inside jPanel ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to add image inside jPanel ??" Watch "How to add image inside jPanel ??" New topic
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: 19696
    
  20

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 - OCEEJBD 6
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: 19696
    
  20

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: 2182
    
    7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add image inside jPanel ??