title: How to write a custom DefaultTableCellRenderer to colorize specific cells and "preserve" the color of the others, Java
body: <p>I'm using netbeans with its gui builder in order to create a desktop application.
  To colorize special cells in my jTables I have implemented a custom DefaultTableCellRenderer based on example code. So far it works.</p>
  
  <p>My problem is the else case of the overwritten function "getTableCellRendererComponent()". I don't want that a cell which is not in the specified range becomes "white" - I want that these cells appear in the color they already had (e. g. I don't want to change there color to "white" if they have been "red").</p>
  
  <p>I would be happy if somebody could help me on that!!</p>
  
  <p>Thanks a lot in advance.
  <BR>Steffen</p>
  
  <pre><code>class ColoredTableCellRenderer extends DefaultTableCellRenderer
  {
  
      /** constructor */
      public ColoredTableCellRenderer(int rowToColorizeStart, int rowToColorizeStop, int columnToColorizeStart, int columnToColorizeStop, Color color)
      {
          mRowToColorizeStart = rowToColorizeStart;
          mRowToColorizeStop = rowToColorizeStop;
          mColumnToColorizeStart = columnToColorizeStart;
          mColumnToColorizeStop = columnToColorizeStop;
          mColor = color;
      }
  
      private int mRowToColorizeStart = 0;
      public void setRowToColorizeStart(int rowToColorizeStart) {
          mRowToColorizeStart = rowToColorizeStart;  
      }
      public int getRowToColorizeStart() {
          return mRowToColorizeStart;  
      }
  
      private int mRowToColorizeStop = 0;
      public void setRowToColorizeStop(int rowToColorizeStop) {
          mRowToColorizeStop = rowToColorizeStop;  
      }
      public int getRowToColorizeStop() {
          return mRowToColorizeStop;  
      }
  
          private int mColumnToColorizeStart = 0;
      public void setColumnToColorizeStart(int columnToColorizeStart) {
          mColumnToColorizeStart = columnToColorizeStart;  
      }
      public int getColumnToColorizeStart() {
          return mColumnToColorizeStart;  
      }
  
      private int mColumnToColorizeStop = 0;
      public void setColumnToColorizeStop(int columnToColorizeStop) {
          mColumnToColorizeStop = columnToColorizeStop;  
      }
      public int getColumnToColorizeStop() {
          return mColumnToColorizeStop;  
      }
  
      private Color mColor = Color.WHITE;
      public void setColor(Color color) {
          mColor = color;  
      }
      public Color getColor() {
          return mColor;  
      }
  
  
      @Override
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
  
          Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);    
  
          if( (row &gt;= getRowToColorizeStart()) &amp;&amp; (row &lt;= getRowToColorizeStop()) &amp;&amp; (column &gt;= getColumnToColorizeStart()) &amp;&amp; (column &lt;= getColumnToColorizeStop()) &amp;&amp; (isSelected == false) ) {
              cellComponent.setBackground(getColor());    
          } else {
             cellComponent.setBackground(Color.WHITE);
          }
  
          return cellComponent;
  
      }
  }
  </code></pre>