I'm trying to color particular rows according to the first column values in JTable
, but the code below colors the rows according to the row's index. My table has simply four columns. The first column has ID numbers. I need to color the rows according to these ID numbers. For example, if the first ID is 0 and the second is also 0, both of them should be "lightGray". Any idea, please?
table_1 = new JTable(){
public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
Component comp = super.prepareRenderer(renderer,Index_row, Index_col);
//even index, selected or not selected
if (Index_row % 2==0 && !isCellSelected(Index_row, Index_col)) {
comp.setBackground(Color.lightGray);
} else {
comp.setBackground(Color.white);
}
return comp;
}
};
Here is how it looks now:
@Override
annotation. – Androclinium