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();
}
});
}
}
PropertyChangeListener
forPanel.background
now, see my code. But it's never notified, not even when I set the color. – Sobriety