繁体   English   中英

Java - Swing - JTable - 为选定行设置颜色,但不为单元格设置颜色

[英]Java - Swing - JTable - Set Color for Selected Row, but not Cell

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

    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() function 更改为使用我想要的 colors。 isSelected()中设置 colors 没有任何作用。

这段代码的问题是:

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

  2. setForegroundColor()用于更改文本,但setBackgroundColor()对单元格背景没有任何作用。 我希望它像属性暗示的那样实际改变背景颜色。

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

    • #3 的解决方案:我想通了! 如果您启用行选择 ( table.setRowSelectionAllowed(true) ),而不是使用仅影响单个单元格的hasFocus() ,那么您将颜色更改放在if(isSelected)语句中。 然后整行被认为是选中的,它是 colors 中的所有单元格!

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

太直接了就加行

tablename.setSelectionBackground(Color.red);

就我而言

jtbillItems.setSelectionBackground(Color.red);

教程文章概念:编辑器和渲染器解释说,“单个单元格渲染器通常用于绘制包含相同类型数据的所有单元格”,如模型的getColumnClass()方法所返回的那样。

或者,覆盖为所有单元格调用的prepareRenderer()以有选择地更改行的外观。 示例比较了这两种方法, 表格行呈现一文扩展了该方法的多功能性。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM