0

I want to add a picture in the background of my entire GUI frame, and then put other components (buttons, text fields etc.) over it. Is it possible with Java Swing?

My question is mainly about making top-level () components sit on the picture in the background (as the title suggests). That is getting a kind of hierarchy in which there is a container at the bottom, an image in the container (stretched to fill the container) and then buttons sitting on it.

Is there a way to do that? Any tips or suggestions will be greatly appreciated.

Solace
  • 8,612
  • 22
  • 95
  • 183
  • 1
    Apart from comments i'd recommed you to use [`imgScalr`](http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/) if you have to resize your image. – nachokk Apr 26 '14 at 15:02
  • @nachokk I'm sorry I probably couldn't explain it well. Now that I have edited my question, you can see that my question is about mainly about putting the components on the image, or rather setting the background image such that the other components sit over it, and not under it. Thank you for pointing me to the library.. – Solace Apr 26 '14 at 16:57

1 Answers1

2
  1. Use a JLabel containing an Icon as the background component of your frame.
  2. Set the layout manager of the JLabel
  3. Add components to the JLabel.

You can also use the Stretch Icon, if you want the image to dynamically scale as the frame resizes.

Or you can also use the Background Panel which will do custom painting for you. It allows you to paint the image at its actual size, scaled or tiled.

camickr
  • 321,443
  • 19
  • 166
  • 288