Monday, June 22, 2009

How to change color of JTable row having a particular value

http://www.java-forums.org/awt-swing/541-how-change-color-jtable-row-having-particular-value.html

-31-2007, 10:08 PM
johnt
Super Moderator Join Date: Apr 2007
Posts: 30


How to change color of JTable row having a particular value
I am trying to change the color of entire row which is having a column of value "FAIL". Here is my code, which is changing only that particular column. But i want to change the entire row.

Can anyone please give me an idea?

Code:
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus,int row,int col)
{

String s = table.getModel().getValueAt(row,col).toString();

if(s.equalsIgnoreCase("Fail")) {
setForeground(Color.red);
}else {
setForeground(null);
}

return super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
row, col);

}

Sponsored Links


#2 (permalink) 05-31-2007, 10:12 PM
levent
Senior Member Join Date: Dec 2006
Posts: 748


If you know the column in the model that the test value will reside in, set it as a constant and your could would end up looking like this.

Code:
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col)
{
Component comp = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, col);

String s = table.getModel().getValueAt(row, VALIDATION_COLUMN ).toString();

if(s.equalsIgnoreCase("Fail"))
{
comp.setForeground(Color.red);
}
else
{
comp.setForeground(null);
}

return( comp );
}

No comments: