GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Background color not inherited from the immediate container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Background color not inherited from the immediate container" Watch "Background color not inherited from the immediate container" New topic
Author

Background color not inherited from the immediate container

seema mani
Ranch Hand

Joined: Sep 30, 2001
Posts: 49
I have heard that for AWT components , if background color is not set explicitly, it is inherited from the immediate container.
In the following program , the button is not inheriting the background color of the frame. If I change it to a label or textfield, the background color is getting inherited.
Can anyone tell me why this does not work for button ?
Thanks in advance.
import java.awt.*;
public class L1 extends Frame {
public L1()
{
setBackground(Color.red);
setLayout(new FlowLayout());
Button b1=new Button("OK");
add(b1);
setSize(100,100);
setVisible(true);
}
public static void main(String args[])
{
new L1();
}
}

Sun Certified Java Programmer<br />Sun Certified Web Component Developer<br />Sun Certified Business Component Developer
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Textfields don't inherit the background color either(at least on Windows NT)... The only widgets I can recall off the top of my head that inherit backgrounds are Labels and Panels... I think it's because buttons are made to look like they are "sticking out of" the GUI and their color sets them apart... and texfields are supposed to look like they are "recessed into" the GUI... also, AWT isn't as platform independent as it could be... so this also may be dependent on what platform you are running on...

If you really need to set all the widget's backgrounds, do something like this... override Frame and Panel to have this method:


-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Looked in my old certification book (Roberts, Heller, Ernest) and here is the warning under setBackground/setForeGround.
Warning: Some components on some platforms resist having their colors changed.
I am using Win 2000 and have to set the background for a Button to get it to work.
 
GeeCON Prague 2014
 
subject: Background color not inherited from the immediate container