java - JTable TableCellRenderer 着色不正确

标签 java swing jtable tablecellrenderer

我使用自定义的 DefaultTableCellRenderer 创建了一个简单的 JTable。它本身工作正常(最后一列着色)。但是,一旦我选择一行或过滤/取消过滤它,该行就会被着色,即使它根本不应该被着色。

我的渲染器:

public class StatusCellRenderer extends DefaultTableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int col) {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                table.convertRowIndexToModel(row), col);
        DataTableModel model = (DataTableModel) table.getModel();
        String data = model.getValueAt(table.convertRowIndexToModel(row), col).toString();
        if (col == 3 && data.equalsIgnoreCase("successful") && !data.isEmpty()) {
            c.setBackground(Color.GREEN);
        }
        if (col == 3 && !data.equalsIgnoreCase("successful") && !data.isEmpty()) {
            c.setBackground(new Color(255, 51, 51));
        }
        return c;
    }
}

它最初的外观(以及它应该始终的外观):

enter image description here

选择 2 行(顶部和底部行)后:

enter image description here

正如您所看到的,有几行是绿色的,根本不应该着色。更令人不安的是,我只选择了绿色 block 的顶行和底行,这意味着它也会自动为中间的行着色。

如何停止这种行为并仅对第一张图片中所示的行进行着色?

<小时/>

接受的答案非常帮助我克服了这些问题,这是最终的代码:

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int col) {
    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
            table.convertRowIndexToModel(row), table.convertColumnIndexToModel(col));
    DataTableModel model = (DataTableModel) table.getModel();
    String data = model.getValueAt(table.convertRowIndexToModel(row), table.convertColumnIndexToModel(col))
            .toString();
    if (!isSelected) {
        if (col == 3 && data.equalsIgnoreCase("successful") && !data.isEmpty()) {
            c.setBackground(Color.GREEN);
        } else if (col == 3 && !data.equalsIgnoreCase("successful") && !data.isEmpty()) {
            c.setBackground(new Color(255, 51, 51));
        } else {
            c.setBackground(Color.WHITE);
        }
    } else {
        c.setBackground(c.getBackground());
    }
    return c;
}

如果单元格被选中,它的颜色为蓝色,如果没有,则根据值,它的颜色为白色、绿色或红色

最佳答案

由于渲染器组件将被重用,请考虑在没有条件匹配时设置默认颜色:

    if (col == 3 && data.equalsIgnoreCase("successful") && !data.isEmpty()) {
        c.setBackground(Color.GREEN);
    }
    else if (col == 3 && !data.equalsIgnoreCase("successful") && !data.isEmpty()) {
        c.setBackground(new Color(255, 51, 51));
    }
    else {
        c.setBackground(Color.GRAY.brighter());
    }

关于java - JTable TableCellRenderer 着色不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50756543/

相关文章:

java - JTable 颜色行和单元格动态

java - 如何使用单个滚动条滚动两个或多个 JTable?

Java FileInput/OutputStream 和 ByteBuffer

java - 提高 Java 中字符串连接的性能

java - Jtable保留列宽

java - 如何用不断变化的tableModel来刷新JTable?

java - 让你的 JFrame 成为静态变量是不是很糟糕? ( java Swing )

java - 通过 JTextField 快速搜索 JTable 中的批量数据

java - 关闭 ANTLR4 CharStream(Java 运行时)- OutOfMemoryException

java - 未设置 libs.CopyLibs.classpath 属性