Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Background color not inherited from the immediate container

 
seema mani
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Stevens
Ranch Hand
Posts: 2823
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic