Monday, August 17, 2009

How to add an image to a JPanel

In case of using Netbeans IDE Palettes for GUI building, in order to insert an image into JPanel, you have to change the layout of panel to "Border Layout". Then you have to use a JLabel to pass the image into JPanel.

I have tried many ways but this worked fine for me:

ImageIcon picturetoInsert = newImageIcon("C:/images/anyimage.gif");
JLabel label = new JLabel("",picturetoInsert,JLabel.CENTER);
pPanel.add(label,BorderLayout.CENTER);




Labels: ,

3 Comments:

At 6:36 PM , Blogger Unknown said...

Really Helpfull. I was looking on the internet for this problem but did not find any solutions, untill i stumbled upon this site. Thank you.

Rishi

 
At 2:27 AM , Blogger old codger said...

Hi, I am new to Java and I am trying to get this code to work. The problem is: it works ok when I run it in Netbeans but all that is displayed in the browser is a black rectangle.
Can you please help???

 
At 1:32 PM , Blogger Wikimechanist said...

Can you send the code part to understand what you are exactly doing?
Did you give the correct path for your picture to insert into jlabel?

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home