簡體   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