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;
}
}
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;
}
}