-
JTable Row Color
Hi all,
in order to set the background color to each row that has some value (1000 for example) in some colon, i made this DefaultTableCellRenderer :
class RowColorRenderer implements TableCellRenderer {
public static final DefaultTableCellRenderer MyDTCR = new DefaultTableCellRenderer();
public Component getTableCellRendererComponent(JTable TheTable,Object TheValue,boolean isSelected,boolean hasFocus,int row,int column)
{ Component renderer = MyDTCR.getTableCellRendererComponent(TheTable, TheValue, isSelected, hasFocus, row, column);
((JLabel) renderer).setOpaque(true);
Color foreground, background;
if (isSelected) {
foreground = Color.yellow;
background = Color.green;
}
else { if (TheTable.getValueAt(row,7).toString().equals("100 0")) {foreground = Color.blue;
background = Color.red;
}
else {foreground = Color.white;
background = Color.blue;
}
}
renderer.setForeground(foreground);
renderer.setBackground(background);
return renderer;
}
}
But, it does NOT work...
Thanks to anyone tell me what is wrong in the code ....
-
Re: JTable Row Color
Sorry, but you telling us that it does not work is as useful to us as us telling you to fix it is to you. If you want help, you'll have to provide an SSCCE that demonstrates what you're talking about, and you'll have to be more specific about what's not working about it. Does it throw an Exception? Does it not display at all? Does it attain sentience and declare war on humanity?
Hint: Don't use == on Strings. Do a search on these forums or google for why not.
-
Re: JTable Row Color
Sorry KevinWorkman... here some details
Simply when i run the code, the background in any row that contains the value 1000 in the colon number 7 in NOT red ...
All the rows have a BLUE background...
i think that the problem is in :
if (TheTable.getValueAt(row,7).toString().equals("100 0"))
many thanks to any help
Sorry again...
-
Re: JTable Row Color
Like I said, don't use == when comparing String. Use the equals() method instead. The == operator, when dealing with Objects, simply compares whether two instances are the same, not whether the values they hold are the same.
-
Re: JTable Row Color
Did you not like this answer for some reason?
Crossposted: JTable Row Color
-
Re: JTable Row Color