加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java – Swing – JTable – 为所选行设置颜色,但不为Cell设置颜色

发布时间:2020-05-24 18:14:42 所属栏目:Java 来源:互联网
导读:我试图让我的表在单击一个单元格时选择整行(这可以通过关闭列选择来完成),但是,我不希望您单击的特定单元格周围的额外粗边框突出显示.我希望这很容易,但显然它涉及渲染器所以我做了很多研究,我能得到的最接近的是: JTable contactTable = new JTable(tableMo

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

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()中设置颜色什么也没做.

这段代码的问题是:

>它仅适用于底部由vColIndex指定的一列.显然,我希望将其应用于所有列,因此单击一个单元格会突出显示整行.我可以创建一个for循环来将其更改为每个单元格,但我认为这是一种更好的方法,可以立即更改所有列的cellRenderer.
> setForegroundColor()用于更改文本,但setBackgroundColor()不对单元格背景执行任何操作.我希望它实际上改变了属性所暗示的背景颜色.

>#2的解决方案:使用this.setOpaque(true);在分配backgroundcolor之前.

>当渲染器工作时,它只适用于单个Cell.如何让它为行中的所有单元格着色?

>#3的解决方案:我明白了!如果启用行选择(table.setRowSelectionAllowed(true)),则将颜色更改放在if(isSelected)语句中,而不是使用仅影响单个单元格的hasFocus().然后整行被认为是选中的,它会为所有细胞着色!

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

解决方法

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

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

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读