title: JTable row color depending on Value in model?
body: <p>I have this code in my Table model:</p>
  
  <pre><code>public class DocumentProjectTableModel extends AbstractTableModel{
  
      private List&lt;MyDocument&gt; myDocuments;
      public String getValueAt(int row, int column) {
              String toReturn = null;
              MyDocument myDocument = myDocuments.get(row);
              SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
  
              switch (column) {
                  case 0:
                      if(myDocument.getProject().getRegDate()!=null) toReturn = format.format(myDocument.getProject().getRegDate());
                      break;
                  case 1:
                      toReturn = myDocument.getProject().getRegNum();
                      break;
                  case 2:
                      toReturn = myDocument.getProject().getDescription();
                      break;
                  case 3:
                      toReturn = myDocument.getProject().getShortName();
                      break;
                  case 4:
                      toReturn = myDocument.getProject().getSecondName()+myDocument.getProject().getFirstName()+myDocument.getProject().getMiddleName();
                      break;
  
              }
              return toReturn;
          }
  //  some other stuff is not shown
  </code></pre>
  
  <p>I want to change background color of each row, for example if <code>myDocument.getIsRegistered() == true</code>, I want this row to have yellow background, if <code>myDocument.getIsValid == false</code> row is blue and so on.</p>
  
  <p>I've found examples that recolor rows depending on values in JTable. But getIsValid and getIsRegistered() aren't actually displayed, they exist only in model. Any advice or example would really help. thanks in advance.</p>
  
  <p>update. my TableCellRenderer:</p>
  
  <pre><code>public class MyTableCellRenderer extends JLabel implements TableCellRenderer {
  
      public Component getTableCellRendererComponent(JTable table, Object value,
              boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
          String actualValue = (String) value;
          // Set the colors as per the value in the cell...
          if(actualValue.equals("lifesucks") ){
              setBackground(Color.YELLOW);
          }
          return this;
      }
  }
  </code></pre>
  
  <p>using renderer:</p>
  
  <pre><code>            int vColIndex = 0;
              TableColumn col = resultTable.getColumnModel().getColumn(vColIndex);
              col.setCellRenderer(new MyTableCellRenderer());
   resultTable.setModel(new DocumentProjectTableModel(docs));
  </code></pre>
  
  <p>table is shown as usual no yellow color. why?</p>
  
  <p>update2.</p>
  
  <pre><code>resultTable=new JTable(new DocumentProjectTableModel(docs)){
              public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
              {
                  Component c = super.prepareRenderer(renderer, row, column);
                  //  Color row based on a cell value
                  if (!isRowSelected(row)) {
                      c.setBackground(getBackground());
                      int modelRow = convertRowIndexToModel(row);
                      String type = (String) getModel().getValueAt(modelRow, 0);
  
                          c.setBackground(Color.GREEN);
                  }
                  return c;
              }
          };
  </code></pre>
  
  <p>table is empty:(</p>