I want to use a different background color for all my JPanels in an application. How can I do that when using Nimbus Look and Feel?
I follow Changing the Color Theme to change the color of components in Nimbus Look and Feel.
It only works sometimes, randomly. If I set a PropertyChagneListener
before I change the color, it is only notified once.
Here is some test code:
public class RedPanels extends JFrame {
public RedPanels() {
JPanel panel = new JPanel();
add(panel);
setPreferredSize(new Dimension(100, 100));
pack();
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
UIManager.getDefaults().addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getPropertyName().equals("Panel.background")) {
System.out.println("color changed");
}
});
UIManager.put("Panel.background", new Color(255,0,0));
break;
}
}
} catch (Exception e) {
// Nimbus is not available.
}
new RedPanels();
}
});
}
}
Looks like a bug in jdk6, Panel.background one of the properties not taken. Following works in jdk7 (note the sequence: first set the color, then the LAF)
My guess is that it's still somehow buggy, as Nimbus is supposed to update its properties on receiving any change in the managers setting, so reversing the sequence to first set Nimbus, then put the color) should work as well, but doesn't even in jdk7
Seems to be specific to Panel.background (and most probably a bunch of others), "control" is okay in both jdks, both before and after setting the LAF.
there are three ways
1) override
nimbusBase
for setDerivedColor
2) create own
Painter
, only one example is there -> aephyr codesource,3) simple and dirty hack to set the Color directly