Ozan Ertürk
Ozan Ertürk

Reputation: 485

JTable cellRenderer doesnt change just desingated cells

Here is a code part of my project. I am try to change the color of desingated cells. But when i try it, all cells' color changing. Why is that ? Thanks.

private class cellRenderer extends DefaultTableCellRenderer {

    @Override
    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);
      if(row==column){
          cell.setBackground(Color.yellow);
      }     
        return cell;
    }
}

Upvotes: 1

Views: 40

Answers (1)

bradimus
bradimus

Reputation: 2533

I think you need to restore the original color.

private class cellRenderer extends DefaultTableCellRenderer {
    Color originalColor = null;

    @Override
    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);


       if (originalColor == null) {
           originalColor = cell.getBackground();
      }

      if(row==column){
          cell.setBackground(Color.yellow);
      } else {
          cell.setBackground(originalColor);
      }

      return cell;
    }
}

Upvotes: 1

Related Questions