title: Nimbus and alternate row colors
body: <p>I don't understand how alternate row coloring works in Nimbus. It seems just crazy!!! I would like to clear things up here.</p>
  
  <p>For the demonstration, let's say that <strong>we want a JTable that alternate Red and Pink rows</strong> (and I don't care which color is the first one).</p>
  
  <p><strong>Without redefining custom cellRenderers</strong> that perform their own "modulo 2" thing, and without overriding any method from JTable, I want to list the mandatory steps between starting one's application and getting a JTable with custom alternate row colors <strong>using Nimbus properties only</strong>.</p>
  
  <p>Here are the steps I expected to follow:</p>
  
  <ol>
  <li>Install the Nimbus PLAF</li>
  <li>Customize the "Table.background" nimbus property</li>
  <li>Customize the "Table.alternateRowColor" nimbus property</li>
  <li>Create a JTable with simple data/header</li>
  <li>Wrap the jTable in a JScrollPane and add it to the JFrame</li>
  <li>Show the JFrame</li>
  </ol>
  
  <p>Here the source code:</p>
  
  <hr>
  
  <pre><code>public class JTableAlternateRowColors implements Runnable {
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new JTableAlternateRowColors());
      }
  
      @Override
      public void run() {
          try {
              UIManager.setLookAndFeel(new NimbusLookAndFeel());
          } catch (UnsupportedLookAndFeelException e) {
              e.printStackTrace();
          }
  
          UIManager.getDefaults().put("Table.background", Color.RED);
          UIManager.getDefaults().put("Table.alternateRowColor", Color.PINK);
  
          final JFrame jFrame = new JFrame("Nimbus alternate row coloring");
          jFrame.getContentPane().add(new JScrollPane(new JTable(new String[][] {
                  {"one","two","three"},
                  {"one","two","three"},
                  {"one","two","three"}
          }, new String[]{"col1", "col2", "col3"}
          )));
          jFrame.setSize(400, 300);
          jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          jFrame.setVisible(true);
      }
  }
  </code></pre>
  
  <p>This is JDK6 code.
  Can somebody tell me goes wrong here?</p>
  
  <hr>
  
  <p>As per @kleopatra's comment and the contribution of the whole community here's a/the way to get alternate row coloring using only Nimbus properties</p>
  
  <p>public class JTableAlternateRowColors implements Runnable {</p>
  
  <pre><code>public static void main(String[] args) {
      SwingUtilities.invokeLater(new JTableAlternateRowColors());
  }
  
  @Override
  public void run() {
      try {
          UIManager.setLookAndFeel(new NimbusLookAndFeel());
      } catch (UnsupportedLookAndFeelException e) {
          e.printStackTrace();
      }
  
      UIManager.put("Table.background", new ColorUIResource(Color.RED));
      UIManager.put("Table.alternateRowColor", Color.PINK);
      UIManager.getLookAndFeelDefaults().put("Table:\"Table.cellRenderer\".background", new ColorUIResource(Color.RED));
  
      final JFrame jFrame = new JFrame("Nimbus alternate row coloring");
      final JTable jTable = new JTable(new String[][]{
              {"one", "two", "three"},
              {"one", "two", "three"},
              {"one", "two", "three"}
      }, new String[]{"col1", "col2", "col3"});
      jTable.setFillsViewportHeight(true);
      jFrame.getContentPane().add(new JScrollPane(jTable));
      jFrame.setSize(400, 300);
      jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      jFrame.setVisible(true);
  }
  </code></pre>
  
  <p>}</p>