【问题标题】:How to change the background color of each second row?如何更改每第二行的背景颜色?
【发布时间】:2014-04-13 15:20:40
【问题描述】:

我尝试更改每一行的背景颜色。问题是只有第一列受到影响。为什么?

    table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
    {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            c.setBackground(row % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE);
            return c;
        }
    });

【问题讨论】:

  • 当您使用final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 创建c 时,column 的值是多少?也许您指向第一列的单元格并将背景设置为那个
  • 您的表格列中有哪些数据类型?
  • 您需要提供更多信息,关于如何创建表,即模型、渲染器、编辑器。
  • “如何改变每一行的背景颜色?” 使用 Nimbus PLAF(是一种方法)。

标签: java swing jtable background-color tablecellrenderer


【解决方案1】:

使用渲染器方法,您需要为表中的每种数据类型编写自定义渲染器。因此,如果您有 String、Data、Integer、Boolean,则需要编写 4 个自定义渲染器。

请参阅Table Row Rendering 了解一种方法,该方法允许您编写一次代码,无论表中有多少数据类型。这种方法会覆盖 JTable 的preparerrenderer(...) 方法。

【讨论】:

    【解决方案2】:

    How to Use Tables 教程的Concepts: Editors and Renderers 部分所述,如果您没有为特定列指定渲染器,则该表将调用表模型的getColumnClass 方法来获取列类型的默认渲染器。

    如果您已覆盖 getColumnClass 方法,那么您的方法可能无法按预期工作。例如:

    DefaultTableModel model = new DefaultTableModel(new Object[]{"Column # 1", "Column # 2"}, 0) {
        @Override
        public Class<?> getColumnClass(int columnIndex) {
            Class columnClass = Object.class;
            switch(columnIndex) {
                case 0: columnClass = String.class; break;
                case 1: columnClass = Boolean.class; break;
            }
            return columnClass;
        }
    };
    

    然后这样做:

    table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {// your code here});
    

    不适用于第二列,因为 getColumnClass 方法将返回 Boolean.class 并且该类有一个默认单元格渲染器 (JCheckBox)。

    在您的情况下,我建议您覆盖 JTable.prepareRenderer() 方法,而不是独立于渲染器类型(JLabelJCheckBox 甚至自定义渲染器)设置行背景颜色:

    JTable table = new JTable(model) {
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            Component c = super.prepareRenderer(renderer, row, column);
            c.setBackground(row % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE);
            return c;
        }
    };
    

    【讨论】:

      【解决方案3】:

      好吧,把我刚刚写的全部废掉。

      尝试 #2:

      table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
          @Override
          public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
              final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
              c.setBackground(row % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE);
              return this;
          }
      });
      

      您需要返回当前对象,而不是 super() 调用的引用。

      【讨论】:

      • 这仅适用于您的表仅包含字符串数据。如果您有日期或整数或布尔值等,它将不起作用。
      猜你喜欢
      • 2020-10-01
      • 2021-12-30
      • 2011-08-06
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多