繁体   English   中英

通过值更改jTable单元格颜色

[英]Changing jTable cell color by value

根据所有示例,我看过我的代码应该可以工作。 为什么不起作用? 如果我指定行和列,则可以更改表格中的单元格颜色,但如果指定值,则无法使用。 这是我所拥有的:

//Custom renderer to color table cells red
//cellValue = 00:00:00 - a LocalTime in the table, so all cells with that value should be red.

 public class MyTableCellRenderer extends DefaultTableCellRenderer {

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            if (value == cellValue) { 
                c.setForeground(Color.red);
            } else {
                c.setForeground(table.getForeground());
            }
            return c;
        }
    }

在我发疯之前请帮助!

if (value == cellValue) 

不要使用“ ==”来比较对象。

相反,您应该使用equals(...)方法:

if (value.equals(cellValue)) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM