【问题标题】:Changing colors in cells when I change the cell of column 5, he doesn't stop here, he change the next cell当我更改第 5 列的单元格时更改单元格中的颜色,他不会停在这里,他会更改下一个单元格
【发布时间】:2017-03-12 20:39:27
【问题描述】:

我在更改单元格颜色时遇到问题,当我更改第 5 列的单元格时,他并没有停在这里,他更改了下一个单元格... 这是我的代码:

public class MyRenderer 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);
        Etudiant E = new Etudiant();

        if (column == 0) {
            int id = E.getAIdEtud(table.getModel().getValueAt(row, 1).toString(), table.getModel().getValueAt(row, 2).toString());
            if (E.IsRoudoublan(id) && E.IsExcclu(id)) {
                c.setBackground(new java.awt.Color(0, 0, 255));
            } else if (E.IsExcclu(id)) {
                c.setBackground(new java.awt.Color(255, 0, 0));
            } else if (E.IsRoudoublan(id)) {
                c.setBackground(new java.awt.Color(20, 200, 0));
            }
        } else if (column == 5) {
            if (Integer.parseInt(table.getModel().getValueAt(row, 5).toString()) >= 3) {
                c.setBackground(new java.awt.Color(20, 200, 20));
            }
        } else {
            c.setBackground(new java.awt.Color(100, 100, 100));
        }

        return c;

    }
}

【问题讨论】:

  • getTableCellRendererComponent 中报告的行/列是视图索引。您正在使用这些来查找模型中的值。使用JTable.convertRowIndexToModelJTable.convertColumnIndexToModel 获取关联的模型索引,用于查找模型中的值...
  • 另外,在column == 5 的部分中,您需要添加一个else 部分来设置字符串为 的情况的颜色
  • 我试过这个选项,但它不起作用
  • I tried this option, but it didn't work - 那么你的代码是错误的。再试一次。此外,您的渲染器不应检查该列。相反,您应该创建 2 个单独的渲染器。然后,您可以将每个渲染器添加到特定列。这将简化 if/else 逻辑,从而更容易发现错误。
  • 如需尽快获得更好的帮助,请发帖minimal reproducible exampleShort, Self Contained, Correct Example

标签: java swing colors tablecellrenderer


【解决方案1】:

我接受了您的 sn-p 并纠正了我发现的所有问题。检查所有带有 cmets 的行。

注意事项:

  • 为所有可能的代码路径提供颜色,您忘记在两个位置提供颜色(请参阅// << provide ELSE part here, eg: 行)
  • TableCellRenderer.getTableCellRendererComponent 方法中报告的索引是视图索引。
  • 索引模型时,需要使用模型索引。您正在使用视图索引来索引模型。这可能会导致问题,例如对表格进行排序、重新排列列或应用行过滤器时。查找末尾带有 cmets 的行进行更正。

public class MyRenderer extends DefaultTableCellRenderer {
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int rowViewId, int columnViewId) {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, rowViewId, columnViewId);
        Etudiant E = new Etudiant();

        int rowModelId = table.convertRowIndexToModel(rowViewId); // << for lookup of values in the model
        int colModelId = table.convertColumnIndexToModel(columnViewId); // << for lookup of values in the model

        if (colModelId == 0) { // << you mean to check if the model index is 0 here
            int id = E.getAIdEtud(table.getModel().getValueAt(rowModelId, 1).toString(), table.getModel().getValueAt(rowModelId, 2).toString()); // << when indexing the model, use model indexes
            if (E.IsRoudoublan(id) && E.IsExcclu(id)) {
                c.setBackground(new java.awt.Color(0, 0, 255));
            } else if (E.IsExcclu(id)) {
                c.setBackground(new java.awt.Color(255, 0, 0));
            } else if (E.IsRoudoublan(id)) {
                c.setBackground(new java.awt.Color(20, 200, 0));
            }
            // << provide ELSE part here, eg:
            else {
                c.setBackground(new java.awt.Color(100, 100, 100));
            }
        } else if (colModelId == 5) { // << you mean to check if the model index is 5 here
            if (Integer.parseInt(table.getModel().getValueAt(rowModelId, 5).toString()) >= 3) { // << when indexing the model, use model indexes
                c.setBackground(new java.awt.Color(20, 200, 20));
            }
            // << provide ELSE part here, eg:
            else {
                c.setBackground(new java.awt.Color(100, 100, 100));
            }
        } else {
            c.setBackground(new java.awt.Color(100, 100, 100));
        }

        return c;
    }
}

【讨论】:

    猜你喜欢
    • 2014-08-24
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    • 2016-12-31
    • 2012-01-23
    • 1970-01-01
    • 2014-09-06
    相关资源
    最近更新 更多