Hello all,
I am having a table cell renderer problem.
My goal is to do a cell renderer which sets the background color of
certain rows (based upon a piece of data in the model) to a special color
to highlight those rows.
I've got it working, almost. However, despite the fact that the special
renderer seems to be doing what I have asked it, some of the cells (not
randomly, but not consistently either) get the default (white) background
color sometimes, and sometimes switch back to the color I want them to
have.
I'm really lost. Your suggestions would be appreciated.
The outer class (named PlayerWindow) implements TableModel and extends
JFrame, and has an array, players, which has the status
information.
My (inner class) special renderer is:
protected class
StatusBackgroundTableCellRenderer
extends
DefaultTableCellRenderer
{
public Component
getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected, boolean hasFocus,
int row, int column)
{
// Get
the default component - but ignore selected/focus
// We
ignore it so it doesn't draw boxes around these components or
//
anything like that (we don't want things to be selectable
Component c = super.getTableCellRendererComponent(table, value,
false, false, row, column);
if
(row < 0)
// It's doing the header; we don't change that
return c;
//
Only for specific cell
PlayerValue pv;
synchronized (PlayerWindow.this) {
pv = players[row];
}
if
(pv.status == PlayerValue.STATUS_AWAITING_TURN) {
c.setBackground(Color.pink);
System.out.println("getTCRC: row " + row + ", col " +
column + ", PINK");
} else
{
c.setBackground(table.getBackground());
System.out.println("getTCRC: row " + row + ", col " +
column + ", REGULAR");
}
//
System.out.println("Class: " + c.getClass());
return
c;
}
}
From this, you can see that it prints out interesting information
(the row, column, and color). These print out on the console the right
answers.
However, cell 0,0, and often 0,1 (row, col) often have the wrong color
displayed (white background) in one test case, but sometimes have the
correct color (if I click on a header cell, etc.).
I'm really stymied.
The initialization code for this is:
playersTable.setDefaultRenderer(Object.class, new
StatusBackgroundTableCellRenderer());
All the columns are actually String columns; Changing that to
String.class does not change anything.
Thanks for your help,
Doug
