title: Changing color of cell in JTable
body: <p>I would like to change the color of a cell in a JTable. I wrote my own class that extends DefaultTableCellRenderer. However, my class has really inconsitent behavior. All it does is if a entry appears twice in a column, it marks it red. This is the result I get:</p>
  
  <p><img src="https://i.stack.imgur.com/3ACs9.png" alt="enter image description here"></p>
  
  <p>Notice that in this class I am also setting the font for a particular column. That works fine. I am wondering why I get this behavior when trying to simply set the color.</p>
  
  <p>Here is my class:</p>
  
  <pre>
  <code>
  /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package inter2;
  
  import java.awt.Color;
  import java.awt.Component;
  import java.awt.Font;
  import java.util.List;
  import javax.swing.JTable;
  import javax.swing.table.DefaultTableCellRenderer;
  
  /**
   * Used to display different fonts for different cells in the table
   */
  public class CustomCellRenderer extends DefaultTableCellRenderer
  {
  
      private final int TRANSLATION_COL = 1;
      private final int VARIABLE_COL = 2;
  
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
              boolean hasFocus, int row, int column)
      {
          Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
          //set it so it can display unicode characters
          if (column == TRANSLATION_COL)
          {
              cell.setFont(new Font("MS Mincho",Font.PLAIN, 12));
          }
          //marks a cell red if it is a duplicate variable name
          if(column == VARIABLE_COL)
          {
              MyTable theTable = (MyTable)table;
              String cellValue = theTable.getValueforCell(row, column);
              boolean dup = false;
              String[] columnData = theTable.getColumnData(column);
              //check if this is already in the list
              for(int i =0; i &lt columnData.length; i++)
              {
                  String currTableValue = columnData[i];
                  if(currTableValue.equals(cellValue) && i != row)
                  {
                      dup = true;
                      break;
                  }
              }
              //we found a dup
              if(dup == true)
              {
                  cell.setBackground(Color.red);
              }
          }
          return cell;
      }
  }
  
  </pre>
  
  <p></code></p>