user1638875
user1638875

Reputation: 105

Setting an image in the background of a JFrame in Java thanks to auto-generated code of Netbeans

I want to set a image background on my JFrame thanks to automated generated code by Netbeans.

Unfortunately, I am facing this exact problem : http://www.areaofthoughts.com/2011/08/netbeans-jframe-properties-iconimage.html

When I tried to add it graphically, on details of iconImage, I have this error : "Custom editing of this property is not supported"

I tried the solution given in the link above, but unfortunately, this code doesn't work:

public ArrayList<Image> getIconImages() {
   ArrayList<Image> imageList = new ArrayList();
   imageList.add(getClass().getResource("/<resource_path>" +
     "/image.png")).getImage());
   return imageList;  
}

What is the problem in this code, and how can I fix this problem of background (by any way?).

Please note that when I did it for label with Netbeans, I had no problem for doing it. The generated code was, for example, the following:

jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/myimage/img.png")));    

Upvotes: 0

Views: 3136

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

You generally have two choices.

1- Create a custom JPanel that can paint the image as part of its background, then add this to your frame (once compiled, it can be dragged into the designer), have a look at Performing Custom Painting

2- Set the layout of the JFrame to BorderLayout, add a JLabel to the frame. Assign a image/icon to the label. Set the layout of the label to what ever you need and your components toit.

You may need to adjust the horizontal & vertical position of the icon within the label to suit your needs

For an example, check out this previous answer Place JLabel on top of JLabel with image in

Upvotes: 1

Related Questions