B
Bryan R. Meyer
I currently have a JTable object that utilizes the DefaultTableModel
and has three columns. For simplicity, I'll call them "Column A",
"Column B", and "Column C" and all are text cells. In a for loop in
my class, I'm testing a flag condition, and I'm setting a global
boolean variable that is equal to that flag. I then add my data to
the table. I have a custom renderer for "Column B" that colors the
text red for that particular row if the flag is true.
Unfortunately, it appears that the cells are not rendered until all of
the iterations of the loop are complete. So, if the last value of
testFlag was true, all of the text in Column B is red. If the last
value was false, all of the text in Column B is the standard color.
Why are these cells not rendered immediately after the addRow method
is called? Is there any way to force the update? I thought my
solution was the easiest to color the individual cell.
Any ideas are appreciated. The code is below.
Thanks,
Bryan
boolean testFlag;
for(int i; i<vector.size(); i++) {
if(test condition is true)
testFlag = true;
else
testFlag = false;
Object[] curMedia = {string1, string2, string3};
data.addRow(curMedia);
}
class ColoredCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object
value,
boolean isSelected, boolean hasFocus, int row, int column) {
if(testFlag) {
setForeground(Color.red);
}
else {
setForeground(table.getForeground());
}
return this;
}
}
and has three columns. For simplicity, I'll call them "Column A",
"Column B", and "Column C" and all are text cells. In a for loop in
my class, I'm testing a flag condition, and I'm setting a global
boolean variable that is equal to that flag. I then add my data to
the table. I have a custom renderer for "Column B" that colors the
text red for that particular row if the flag is true.
Unfortunately, it appears that the cells are not rendered until all of
the iterations of the loop are complete. So, if the last value of
testFlag was true, all of the text in Column B is red. If the last
value was false, all of the text in Column B is the standard color.
Why are these cells not rendered immediately after the addRow method
is called? Is there any way to force the update? I thought my
solution was the easiest to color the individual cell.
Any ideas are appreciated. The code is below.
Thanks,
Bryan
boolean testFlag;
for(int i; i<vector.size(); i++) {
if(test condition is true)
testFlag = true;
else
testFlag = false;
Object[] curMedia = {string1, string2, string3};
data.addRow(curMedia);
}
class ColoredCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object
value,
boolean isSelected, boolean hasFocus, int row, int column) {
if(testFlag) {
setForeground(Color.red);
}
else {
setForeground(table.getForeground());
}
return this;
}
}