首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java JTable TableCellRenderer问题

Java JTable TableCellRenderer问题
EN

Stack Overflow用户
提问于 2010-03-04 09:22:17
回答 1查看 1K关注 0票数 0

我已经在程序中实现了一个名为scrTbl的JTable,我希望能够基于一个名为"up“的外部布尔变量来改变该表的一列中的文本颜色。与此工作相关的代码如下所示。

代码语言:javascript
复制
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列中的单元格时都会调用渲染器,或者协议是什么?

提前感谢您的帮助。

EN

回答 1

Stack Overflow用户

发布于 2010-07-10 04:30:37

由于您只想修改一列,因此请调整代码以指定列和行

代码语言:javascript
复制
    if (row == nmbrStocks && column == the_desired_column_you_wish_to_change)
    {
      if (up){
        cell.setForeground(Color.green);
      }else{
        cell.setForeground(Color.red);
      }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2376280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档