We have a program that can change between "daytime" (high-contrast) and "nighttime" (low-contrast) modes. This is done by changing the look-and-feel on the fly. It works for almost all components, but a few are ignoring the background color changes. In particular, I've noticed buttons, comboboxes, and table headers all ignore the change in the PLAF.
Here is an example program with a button. Am I doing something wrong? Is this an OS-dependent behavior somehow? (I'm on OSX)
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.plaf.ColorUIResource;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class Demo extends JFrame {
public static void main(String[] args) {
new Demo();
}
public Demo() {
setTitle("PLAF button test");
final Demo thisWindow = this;
final JButton button = new JButton("Click me!");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Color curColor = UIManager.getColor("Button.background");
ColorUIResource targetColor;
if (curColor.equals(new Color(32, 32, 32))) {
targetColor = new ColorUIResource(192, 192, 192);
}
else {
targetColor = new ColorUIResource(32, 32, 32);
}
System.out.println("Setting new color to " + targetColor +
" because old color was " + curColor);
UIManager.put("Button.background", targetColor);
SwingUtilities.updateComponentTreeUI(thisWindow);
}
});
add(button);
setMinimumSize(new java.awt.Dimension(200, 200));
setVisible(true);
}
}
And here is the output I get when I run this program and click on the button:
Setting new color to javax.swing.plaf.ColorUIResource[r=32,g=32,b=32] because old color was com.apple.laf.AquaImageFactory$SystemColorProxy[r=238,g=238,b=238]
Setting new color to javax.swing.plaf.ColorUIResource[r=192,g=192,b=192] because old color was javax.swing.plaf.ColorUIResource[r=32,g=32,b=32]
Setting new color to javax.swing.plaf.ColorUIResource[r=32,g=32,b=32] because old color was javax.swing.plaf.ColorUIResource[r=192,g=192,b=192]
Setting new color to javax.swing.plaf.ColorUIResource[r=192,g=192,b=192] because old color was javax.swing.plaf.ColorUIResource[r=32,g=32,b=32]
So the UIManager believes the color is changing, but the apparent color on-screen does not change.
Any advice appreciated. Thank you for your time.