title: Java Nimbus Look and Feel per component customization ("Nimbus.Overrides") - other instances affected too
body: <p>I am new to <strong>Java Nimbus Look and Feel</strong>. I am trying to use the ability of Nimbus to <strong>customize individual component instances</strong> using <code>putClientProperty("Nimbus.Overrides", overrides)</code>: <a href="https://docs.oracle.com/javase/8/docs/api/javax/swing/plaf/nimbus/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/javax/swing/plaf/nimbus/package-summary.html</a></p>
  
  <p>I have encountered the following problem (bug?):</p>
  
  <p><strong>"Nimbus.Overrides" value unfortunately affects apparently not only the component object to which it is explicitely set, but other objects too.</strong></p>
  
  <p>It seems that the customized properties are "inherited" somehow to other (later "styled", apparently not previously) instances of the same type. I need to ensure that the changes are made only to one individual instance (without affecting any other objects). Example - <code>JButton</code> used, but the same problem encountered with e.g. <code>JTabbedPane</code> and custom painters:</p>
  
  <ul>
  <li><code>button 1</code> - property A (content margins) customized.</li>
  <li><code>button 2</code> - only property B (font) customized - but change in property A visible too (bigger manrgins), "somehow inherited" from <code>button 1</code>.</li>
  <li><code>button 3</code> - no property customized (empty property map used) - both changes (A+B) seem to be inherited (bigger margins, bigger font) from <code>button1</code> and <code>button2</code></li>
  <li><p><code>button 4</code> - default look o JButton (no customization)</p>
  
  <pre><code>import java.awt.FlowLayout;
  import java.awt.Font;
  import java.awt.Insets;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.UIDefaults;
  
  
  public class NimbusPerComponentTest extends JFrame {
  
  public NimbusPerComponentTest()  {
      super("Test");
      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      setLayout(new FlowLayout());
  
      // 4 buttons to test
      JButton button1 = new JButton("Button 1");
      JButton button2 = new JButton("Button 2");
      JButton button3 = new JButton("Button 3");
      JButton button4 = new JButton("Button 4");
      add(button1);
      add(button2);
      add(button3);
      add(button4);
      pack();
  
      // style        
      // button 1
      UIDefaults overrides1 = new UIDefaults();
      overrides1.put("Button.contentMargins", new Insets(10,10,10,10));        
      button1.putClientProperty("Nimbus.Overrides", overrides1);
      // button 2
      UIDefaults overrides2 = new UIDefaults();        
      overrides2.put("Button.font", new Font("Sans Serif", Font.BOLD, 15));
      button2.putClientProperty("Nimbus.Overrides", overrides2);
      // button 3
      UIDefaults overrides3 = new UIDefaults();
          // nothing = left empty
      button3.putClientProperty("Nimbus.Overrides", overrides3);
      // button 4
      // no styling        
  
  }
  
  public static void main(String args[]) {        
      try {
          for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
              if ("Nimbus".equals(info.getName())) {
                  javax.swing.UIManager.setLookAndFeel(info.getClassName());
                  break;
              }
          }
      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
          java.util.logging.Logger.getLogger(NimbusPerComponentTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      }       
      java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
              new NimbusPerComponentTest().setVisible(true);
          }
      });
  }
  }            
  </code></pre></li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/PNKWk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PNKWk.png" alt="enter image description here"></a></p>
  
  <p>Any idea WHY? What am I missing? Any elegant workarounds?
  (Java 8, Windows 10)</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After inspiration by some of the answers:</p>
  
  <p>Tried reseting the lookandfeel in the end of my original code (to null and back to Nimbus again, incl. SwingUtilities.updateComponentTreeUI), and the only result:</p>
  
  <p>now even the button4 is painted wrong (with both margins and font changed) although the universal defaults were never touched... Weird. </p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I managed to find a <strong>single line workaround /hack</strong>. See my own answer to my question...</p>