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: Java, Programming
3 Comments:
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
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???
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