Java JTable TableCellRenderer 问题

发布于 2024-08-24 12:39:27 字数 1015 浏览 2 评论 0原文

我在程序中实现了一个名为 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 列中的单元格时是否都会调用渲染器,或者协议是什么?

预先感谢您的任何帮助。

I have implemented a JTable named scrTbl in a program, and I wish to be able to vary text color in one column of this table, based on an external boolean variable called "up". My code related to this effort is as follows.

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...

The point is to set the text color for column 9 and a particular row (indexed nmbrStocks) to green or red, based on the value of up.

But when it runs, it sets all of the text green. Is the renderer called each time a cell in column 9 is written to, or what is the protocol?

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彡翼 2024-08-31 12:39:27

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

    if (row == nmbrStocks && column == the_desired_column_you_wish_to_change)
    {
      if (up){
        cell.setForeground(Color.green);
      }else{
        cell.setForeground(Color.red);
      }
    }

Since you only want to modify one column, adjust your code to specify the column as well as the row

    if (row == nmbrStocks && column == the_desired_column_you_wish_to_change)
    {
      if (up){
        cell.setForeground(Color.green);
      }else{
        cell.setForeground(Color.red);
      }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文