0

I know this has probably been ask one billion times, but I am still finding it difficult in getting a straight forward answer.

Where do you put the code under? Can you just add it through the GUI builder-if so how? Or do you have to 'manually' add it in the code? If so do you put it under public class or just class? How to you write it?

Though I would personally prefer if there was a way to add the photo through the GUI builder.

Also, if I added an imagine to a JLabel, would it be possible for me to set it as a background so all the other JLabels or Buttons etc in the GUI is overlapping the picture?

Netbeans version 6.9.1

user2407152
  • 49
  • 2
  • 9
  • possible dupricate of [Java Swing: how to add an image to a JPanel?](http://stackoverflow.com/questions/299495/java-swing-how-to-add-an-image-to-a-jpanel?rq=1) – Shaddow Jun 13 '13 at 12:14
  • 1
    I don't think this is a duplicate since that refers on how it can be done in NetBeans IDE – MaVRoSCy Jun 13 '13 at 13:50

1 Answers1

0

In Netbeans it is a little bit difficult to do this but still it could be done (not as easy as VS). You just have to follow the following steps:

  1. Create the new JPanel object using the wizard
  2. Go to the Source mode and paste the following text

-

public NewJPanel() { //this is the contsructor , so change the name apropriately
    try {
        image = ImageIO.read(new File("c:\\1.png")); //path to your image
    } catch (IOException ex) {
    }
    initComponents();
}
private BufferedImage image;

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(image, 0, 0, null);  //image drawing properties
}
  1. Import all apropriate libs

  2. Save the NewJPanel file.

  3. Now go to your JFrame and drag and drop a Panel Object from the Swing Container list

  4. Right click on the new jPanel object and select Customize Code from the menu

  5. In the Code Customizer box select Custom creation and enter the following code. see image below

    jPanel1 = new NewJPanel();

  6. By doing this you replace the standard JPanel object with the one you created in the first steps

  7. Click ok and then run your JFrame. You should see the image inside the JPanel now

enter image description here

PS: My Netbeans version is 7.2.1

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125