This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes setting image as a background Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "setting image as a background" Watch "setting image as a background" New topic

setting image as a background

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Is it possible to set the background of a JPanel with an ImageIcon?
I have a JPanel that I want to put a JPEG on, as well as two JButtons. But I can't seem to find a way to put the buttons on top of the image.
I thought this may be a possibility? If not, could someone please give me some other ideas?
Greg Charles

Joined: Oct 01, 2001
Posts: 2935

I'm sure you'll get a better answer through here, but what I would do is:
1. Set your image onto a JPanel (jpanel1)
2. Set layout of jpanel1 to BorderLayout.
3. Add another JPanel (jpanel2) to the center of jpanel1.
4. jpanel2.setOpaque(false);
5. Add buttons or whatever to jpanel2.
If you add other panels to jpanel2, make sure to setOpaque to false on them as well.
I agree. Here's the link:
subject: setting image as a background
jQuery in Action, 3rd edition