Posted Wednesday, September 29, 2004 @ 13:54:44
You have to use the CellPropertiesModel to change the cell foreground/background colors. The following is a sample implementation where the rows that to be highligted are stored in a Set object, if the row is in the Set, then red color will be applied to the row, if not the default color will be applied to the row.
class RowHighLighter extends CellPropertiesModel
{
HashSet rowsToBeHighligtedSet = new HashSet();
public void addRowsToBeHighlighted(int row)
{
rowsToBeHighligtedSet.add(new Integer(row));
}
public void removeRows(int row)
{
rowsToBeHighligtedSet.remove(new Integer(row));
}
public Color getForeground(int row, int col)
{
if( rowsToBeHighligtedSet.contains(new Integer(row)) )
return color.red;
else
{
//in all other cases let the cell have the default color
return null;
}
}
}
//apply the model to your dbtable
RowHighLighter rowHighLighter = new RowHighLighter()
dbTable.setCellPropertiesModel(rowHighLighter);
//add/remove rows to be highlighted
rowHighLighter.addRowsTBeHighlighted(1);
//if the colors are not applied once the above method is called,
//uncomment the following two lines
//dbTable.validate();
//dbTable.repaint();