0
我在一个程序中实现了一个名为scrTbl的JTable,我希望能够根据名为“up”的外部布尔变量来改变此表的一列中的文本颜色。我的代码与这项工作相关如下。Java JTable TableCellRenderer问题
TableColumn tcol = scrTbl.getColumnModel().getColumn(9);
tcol.setCellRenderer(new CustomTableCellRenderer());
public class CustomTableCellRenderer extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent (JTable table,
Object obj, boolean isSelected, boolean hasFocus, int row, int
column)
{
Component cell = super.getTableCellRendererComponent(table,
obj, isSelected, hasFocus, row, column);
if (up && (row == nmbrStocks))
{
cell.setForeground(Color.green);
}
if ((!up) && (row == nmbrStocks))
{
cell.setForeground(Color.red);
}
return cell;
}//Component
} //class getTableCell...
的点是设置为塔9和一个特定行(索引nmbrStocks)到绿色或红色的文本颜色,根据最多的值。
但是当它运行时,它将所有文本设置为绿色。每当第9列中的某个单元格被写入时,渲染器是否被调用,或者协议是什么?
在此先感谢您的帮助。