A Java JTable row striping cell renderer

I just found the following source code that shows how to add row striping colors to a Java JTable.

package com.devdaily.heidi;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;

/**
 * I finally got this to work by implementing the checkbox renderer as shown below,
 * but in the end I'm not sure I like the look.
 */
public class StripedRowTableCellRenderer extends DefaultTableCellRenderer
{

  // i didn't like this color
  //private static final Color STRIPE = UIManager.getColor("textHighlight");
 
  // choose whatever color you prefer
  private static final Color STRIPE = new Color(0.929f, 0.953f, 0.996f);
  private static final Color WHITE = UIManager.getColor("Table.background");
 
  private final JCheckBox ckb = new JCheckBox();
 
  public StripedRowTableCellRenderer() {
    setOpaque(true); //MUST do this for background to show up.
  }

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
  {
    JComponent c = (JComponent)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
    if (!isSelected)
    {
      if (row % 2 == 0)
      {
        c.setBackground(WHITE);
      }
      else
      {
        c.setBackground(STRIPE);
      }
    }
    
    if (value instanceof Boolean) { // Boolean
      ckb.setSelected(((Boolean) value));
      ckb.setHorizontalAlignment(JLabel.CENTER);
      ckb.setBackground(super.getBackground());
      if (isSelected || hasFocus) {
          ckb.setBackground(table.getSelectionBackground());
      }
      return ckb;
    }
    
    
    return c;
  }

}

That code will alternate the JTable background colors for each row. That is, even number rows will have one color, and odd number rows will have a different background color.

Applying the JTable cell renderer

To use this class, you need to create an instance of it and then make it the cell renderer for the different objects that are shown in your JTable, something like this:

soundsTable.setDefaultRenderer(Object.class, new StripedRowTableCellRenderer());
soundsTable.setDefaultRenderer(Boolean.class, new StripedRowTableCellRenderer());

(For my own reference, so I can find all of the related source code, I used this code in my original Hyde project, and these last two lines of code are in a file named SoundFileController.java in that project.)