我已经在程序中实现了一个名为scrTbl的JTable,我希望能够基于一个名为"up“的外部布尔变量来改变该表的一列中的文本颜色。与此工作相关的代码如下所示。
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...重点是根据up的值将第9列和特定行(索引为nmbrStocks)的文本颜色设置为绿色或红色。
但当它运行时,它会将所有文本设置为绿色。是否每次写入第9列中的单元格时都会调用渲染器,或者协议是什么?
提前感谢您的帮助。
发布于 2010-07-10 04:30:37
由于您只想修改一列,因此请调整代码以指定列和行
if (row == nmbrStocks && column == the_desired_column_you_wish_to_change)
{
if (up){
cell.setForeground(Color.green);
}else{
cell.setForeground(Color.red);
}
}https://stackoverflow.com/questions/2376280
复制相似问题