title: How to change the background color for JPanels with Nimbus Look and Feel?
body: <p>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?</p>
  
  <p>I follow <a href="http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/color.html" rel="nofollow">Changing the Color Theme</a> to change the color of components in Nimbus Look and Feel.</p>
  
  <p>It only works sometimes, <strong>randomly</strong>. If I set a <code>PropertyChagneListener</code> before I change the color, it is only notified <strong>once</strong>.</p>
  
  <p>Here is some test code:</p>
  
  <pre><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();
          }
      });
    }
  }
  </code></pre>