【问题标题】:Java - Swing - JTable - Set Color for Selected Row, but not CellJava - Swing - JTable - 为选定行设置颜色,但不是单元格
【发布时间】:2012-04-18 12:55:45
【问题描述】:

我试图让我的表格在您单击一个单元格时选择整行(这可以通过关闭列选择来完成),但是,我不希望您单击的特定单元格周围有额外的粗边框被突出显示。我希望这会很容易,但显然它涉及渲染器,所以我做了很多研究,我能得到的最接近的是:

    JTable contactTable = new JTable(tableModel);

    contactTable.setCellSelectionEnabled(true);
    contactTable.setColumnSelectionAllowed(false);
    contactTable.setRowSelectionAllowed(false);
    contactTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    // This renderer extends a component. It is used each time a
    // cell must be displayed.
    class MyTableCellRenderer extends JLabel implements TableCellRenderer {
        // This method is called each time a cell in a column
        // using this renderer needs to be rendered.
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
            // 'value' is value contained in the cell located at
            // (rowIndex, vColIndex)

            if (isSelected) {
                // cell (and perhaps other cells) are selected

            }

            if (hasFocus) {
                // this cell is the anchor and the table has the focus
                this.setBackground(Color.blue);
                this.setForeground(Color.green);
            } else {
                this.setForeground(Color.black);
            }

            // Configure the component with the specified value
            setText(value.toString());

            // Set tool tip if desired
            // setToolTipText((String)value);

            // Since the renderer is a component, return itself
            return this;
        }

        // The following methods override the defaults for performance reasons
        public void validate() {}
        public void revalidate() {}
        protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
        public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
    }

    int vColIndex = 0;
    TableColumn col = contactTable.getColumnModel().getColumn(vColIndex);
    col.setCellRenderer(new MyTableCellRenderer());

我从示例中复制了渲染器,只更改了 hasFocus() 函数以使用我想要的颜色。在isSelected() 中设置颜色没有任何作用。

这段代码的问题是:

  1. 它只适用于底部 vColIndex 指定的一列。显然,我希望将其应用于所有列,因此单击一个单元格中的一个单元格会突出显示整行。我可以创建一个 for 循环来将其更改为每个单元格,但我认为有更好的方法可以一次更改所有列的 cellRenderer。

  2. setForegroundColor() 用于更改文本,但setBackgroundColor() 对单元格背景没有任何作用。我希望它能够真正改变属性所暗示的背景颜色。

    • #2 的解决方案:在分配背景色之前使用this.setOpaque(true);
  3. 当渲染器工作时,它只在单个 Cell 上工作。如何让它为行中的所有单元格着色?

    • #3 的解决方案:我想通了!如果启用行选择 (table.setRowSelectionAllowed(true)),则不要使用仅影响单个单元格的 hasFocus(),然后将颜色更改放在 if(isSelected) 语句中。然后将整行视为已选中,并为其中的所有单元格着色!

3 是一个大的,但如果有人知道 #1 或者可以向我解释为什么它的设计使得您一次只能将渲染器应用于一列,那将不胜感激。

【问题讨论】:

    标签: java swing colors jtable cell


    【解决方案1】:

    对于第二个问题,您可以尝试 setSelectionBackground(Color) 和 setSelectionForeGround(Color) 方法。我不确定您如何解决第一个问题。最后一个建议您可以使用一些摇摆设计器插件,例如 JBuilder。会有很大帮助的。

    【讨论】:

    • 感谢您的回复。我尝试使用 setSelectionBackground(Color) 但 Eclipse 将其标记为不存在的方法。
    • 我现在尝试了 setSelectionBackGround 方法,它可以工作。当我选择一行或多行时,它们的颜色会改变。但也许我理解错了你在找什么。
    • 你可以像使用contactTable.setSelectionBackground(Color.DARK_GRAY);至少它对我有用。
    • 哦,原来如此,我还以为是Component的方法。正确使用它效果很好,除了整行突出显示之外,特定单元格仍然有一个额外的边框。使用不同的渲染器的主要目的只是为了摆脱特定的单元格边框并让整行仍然突出显示。
    【解决方案2】:

    教程文章Concepts: Editors and Renderers 解释说,“单个单元格渲染器 通常用于绘制包含相同类型数据的所有单元格”,由模型的getColumnClass() 方法返回.

    或者,覆盖为所有单元格调用的prepareRenderer(),以选择性地更改行的外观。此example 比较了这两种方法,而文章Table Row Rendering 扩展了该方法的多功能性。

    【讨论】:

      【解决方案3】:

      太直接了就加一行

      tablename.setSelectionBackground(Color.red);
      

      就我而言

      jtbillItems.setSelectionBackground(Color.red);
      

      【讨论】:

      • 仅当您没有设置背景颜色的 TableCellRenderer 时。在这种情况下,您必须添加一个if(!isSelected) {setBackground(color1);}
      猜你喜欢
      • 2011-11-03
      • 2014-09-24
      • 2019-05-09
      • 2013-01-10
      • 2017-09-11
      • 2011-08-06
      • 2019-09-09
      • 2012-10-03
      相关资源
      最近更新 更多