? regarding backgrounds in java GUIs

Status
Not open for further replies.

Venom

Wise, Aged Ars Veteran
193
I'm wondering how I can set a picture to be the background for one of my panels or containers? I am fairly new at Java, and have searched the net a bit, but couldn't find out how to do this.
Basically, my gui is panels within panels, within a container. I have a whole bunch of panels, that I add to two main panels. Then, I add the 2 main panels to the container. How can I make that container have a background? Is this possible at all? If anyone could point me in the right direction, I'd be greatful. If need be, I can clarify my problem further, or post my code.
Thanks for any help!
 
I'm not sure I quite understood what you wanted. Do you want to have a panel that has an image as the background and other stuff in the panel as well, or just an image and nothing else? If its the former I don't think (don't know/haven't seen) it can be done. If its the later I think the way I usually see it done is using an icon for a label (I think).

Of course I haven't looked into this to research my own answer so be sure to take everything with a spoonful of salt (or is that sugar?)

gen
 

wb

Ars Legatus Legionis
25,208
Subscriptor
Basically, my gui is panels within panels, within a container. I have a whole bunch of panels, that I add to two main panels. Then, I add the 2 main panels to the container. How can I make that container have a background? Is this possible at all?

It's possible to give the container a background by overriding its paint() method, but by the way you describe the layout, I don't think you'd see it, since the container is "covered up" by all the panels. Which layout manager are you using?
 

Venom

Wise, Aged Ars Veteran
193
Originally posted by generator:

Do you want to have a panel that has an image as the background and other stuff in the panel as well,

That is exactly what I want to do.
I'm using BorderLayout.
Here is a bit of my code:

JFrame frame = new JFrame("Climate Control System");
Container theGUI = frame.getContentPane();
theGUI.setLayout(new BorderLayout(10, 10));

Now, all the panels I have are added to theGUI. I would like to know if it is possible to give theGUI a background?
 

gumbo

Ars Tribunus Angusticlavius
6,107
I'm back. :)

You have 2 main options. One is to override theGUI's paint method like this:

Code:
public void paint(Graphics g) {
    g.drawImage(createdGraphic);
}

I think that would work, but Sun's Swing tutorial (perhaps the Graphics2d one) has a complete example. I don't have time to look at the docs right now to make sure.

The other way is to create an icon, add it to a label, and add that label to the panel. It sounds like this method may not work, but anyway:

Code:
IconImage image = new IconImage(imagePath);
JLabel label = new JLabel(image);
theGUI.add(label, BorderLayout.CENTER);

This would work if you used a JLayeredPane to add the other components on top.

gubmo

[This message was edited by gubmo on April 04, 2002 at 17:39.]
 
Status
Not open for further replies.