recursive background color working except for jcombobox & jbutton
I'll start this as I usually do: I probably missing somethign obvious but...
I am trying to setup a gui where all of a JFrames child components change background and foreground color. It seems to be working fine except for my JCombobox (the currently selected item background, not the pulldown), my button backgrounds, my checkbox boxes, and my radio button circles. (all of these are appearing as white) (code below)
My suspicion is that I'll need to bail on this approach and look into making a custom laf. I have a strong suspicion that if at all possible, I don't want to go down that route: Is there any way I can make this approach work?
//this is code in my custom JFrame subclass
publicvoid setComponentsColor ( Color foreColor, Color backColor ){
setContainersComponentsBackground( this.getContentPane(), foreColor, backColor );
}
protectedvoid setContainersComponentsBackground ( Container container, Color foreColor, Color backColor ){
Component [] array = container.getComponents();
for(int i = 0; i < array.length; i++ ){
array[i].setBackground( backColor );
array[i].setForeground( foreColor );
//test to see if it is a container
if( Container.class.isAssignableFrom( array[i].getClass()) ){
setContainersComponentsBackground( (Container)(array[i]), foreColor, backColor );
}
}
}
thanks all
-sss
[1945 byte] By [
oldmicaha] at [2007-9-30 2:24:31]

Well, assuming your JComboBox is editable, then you need to change the colors of the editor component, not the rendering component (setBackground/Foreground work on the rendering component). So your code could be something like:
if (array[i] instanceof JComboBox)
JComboBox comboBox = (JComboBox)array[i];
if (comboBox.isEditable())
comboBox.getEditor().getEditorComponent().setBackground( backColor );
comboBox.getEditor().getEditorComponent().setForeground( foreColor );
else
....
Unfortunately, the jcombobox is not editable. (Though I tried setting editor component just to make sure. :)
So to be clear, when the jframe comes up, the jcombobox (and other controls I mentioned ) are white. When I click on the jcombobox, the pulldown's background and foreground (text) color is correct, but the rect where the selected value appears is white.
> So to be clear
To be clear you should include code the demonstates the incorrect behaviour. When I run the following code the North combobox is red and the South combobox is blue. It doesn't matter whether the dropdown is visible or not.
import java.awt.*;
import javax.swing.*;
public class ComboBoxTemp extends JFrame
{
public ComboBoxTemp()
{
String[] items = { "a", "b", "c" };
JComboBox comboBox1 = new JComboBox( items );
comboBox1.setBackground( Color.red );
comboBox1.setForeground( Color.white );
getContentPane().add( comboBox1, BorderLayout.NORTH );
JComboBox comboBox2 = new JComboBox( items );
comboBox2.setEditable( true );
comboBox2.getEditor().getEditorComponent().setBackground(Color.blue);
comboBox2.getEditor().getEditorComponent().setForeground(Color.white);
comboBox2.setBackground( Color.blue );
comboBox2.setForeground( Color.white );
getContentPane().add( comboBox2, BorderLayout.SOUTH );
}
public static void main(String[] args)
{
ComboBoxTemp frame = new ComboBoxTemp();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
When I try your code, I get the editable combobox with the correct background color. The uneditable one (the one I really need to look correct) has the white background. The setForeground does seem to work fine.
see these images:
http://homepage.mac.com/sstrange/POV/comboboxclosed.jpg
http://homepage.mac.com/sstrange/POV/comboboxopen.jpg
My suspicion is that the native look at feel is clobbering my setBackground call. Any pointers on how to work around this?
thanks
That is slightly encouraging in that my Windows testing should go a bit easier.
For now, I'm bailing on the native look and feel and going with customizing the BasicUI objects. Doing it this way does pay attention to my setBackground and setForeground calls. i.e.
public class LootButton extends JButton {
public LootButton ( String label ) {
super( label );
setup();
}
public LootButton ( Icon label ) {
super( label );
setup();
}
protected void setup () {
BasicButtonUI bui = new BasicButtonUI();
setUI( bui );
setBorder( BorderFactory.createRaisedBevelBorder( ) );
}
}
Thanks for the suggestions!