0

I want to add a background image to a JFrame which doesn't have any panels. It is a project I'm working on and I have almost completed it. So, I can't add a background using a JLabel because I will have to change a lot of code to do that and also I'm using netbeans. Is there any solution for this?

pahan
  • 567
  • 1
  • 8
  • 22
  • 3
    possible duplicate of [Setting background images in JFrame](http://stackoverflow.com/questions/1064977/setting-background-images-in-jframe) – Madhawa Priyashantha Sep 15 '15 at 14:04
  • http://stackoverflow.com/questions/18777893/jframe-background-image duplicate – Michele Lacorte Sep 15 '15 at 14:04
  • *"..and also I'm using netbeans"* My condolences. Don't get me wrong. In the hands of someone that understands Swing, it is a great productivity tool. But that is apparently not the case here, and it is just 'getting in the way of getting the job done'. – Andrew Thompson Sep 15 '15 at 14:06

1 Answers1

1

I want to add a background image to a JFrame which doesn't have any panels.

The content pane of the frame is a JPanel, so yes it does have panels.

I have almost completed it. So, I can't add a background using a JLabel because I will have to change a lot of code

If you want a background image then you will need to change your code to make sure the content pane can display the image. So yes you will need to change your code whether you use a JLabel of a JPanel that paints an image.

Check out Background Panel for code that will allow you to use either approach.

The key is that you need to set the content pane of your frame BEFORE you start adding components to the frame. So the code might look something like:

BackgroundPanel panel = new BackgroundPanel( yourImage );
frame.setContentPane( panel );
frame.add(northPanel, BorderLayout.PAGE_START);
frame.add(centerPanel, BorderLayout.CENTER);

I don't know what the Netbeans generated code looks like so I'll leave it up to you to figure out where to put the code.

camickr
  • 321,443
  • 19
  • 166
  • 288