ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-TableCellRenderer将颜色设置为许多单元,而不仅仅是一个

2019-10-28 16:14:09  阅读:308  来源: 互联网

标签:swing jtable jlabel tablecellrenderer java


我有一个JTable,希望我可以更改单击的单个单元格的颜色.

这是我的代码的简化版本:

public class TableFrame extends JFrame {

    public TableFrame() {
        JTable table = new JTable(8, 8);
        table.setGridColor(Color.BLACK);
        table.setDefaultRenderer(CustomCellRenderer.class, new CustomCellRenderer());
        getContentPane().add(table);
    }

    public class CustomCellRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (hasFocus) {
                l.setBackground(Color.red);
                l.setText("Hello");
            }
            return l;
        }
    }
}

当我单击某个单元格时,我希望它可以将颜色更改为红色并向其中添加“ Hello”.它会更改文本,但是出于某些奇怪的原因,它会更改之后的所有单元格的颜色吗?当我单击一个未着色的单元格时,它会执行相同的操作,但是如果有意义的话,并不一定总是以一种有组织的方式进行?就像,它不会为之后的所有单元格上色,但也许是一些刚好在上面的单元格,而其他则保持空白.

这真的很奇怪,毫无意义.怎么了??

解决方法:

在仔细研究DefaultTableCellRenderer类之后,当您在JLabel组件上调用setBackground时,该组件将支持DefaultTableCellRenderer,它将存储您使用的值.

public void setBackground(Color c) {
    super.setBackground(c);
    unselectedBackground = c;
}

再次绘制单元格时,将使用此值(unselectedBackground)在“默认”模式下重新绘制单元格…

    if (isSelected) {
        //...
    } else {
        Color background = unselectedBackground != null
                                ? unselectedBackground
                                : table.getBackground();
        if (background == null || background instanceof javax.swing.plaf.UIResource) {
            Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
            if (alternateColor != null && row % 2 != 0) {
                background = alternateColor;
            }
        }
        super.setForeground(unselectedForeground != null
                                ? unselectedForeground
                                : table.getForeground());
        super.setBackground(background);
    }

这意味着,当您使用setBackground并将其传递给Color.RED时,DefaultTableCellRenderer会假定它成为所有未选中单元格的默认颜色.

您唯一的选择是手动重置背景颜色,例如…

public class CustomCellRenderer extends DefaultTableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (hasFocus) {
            l.setBackground(Color.red);
            l.setText("Hello");
        } else if (!isSelected) {
            l.setBackground(table.getBackground());
        }
        return l;
    }
}

另外,您实际上应该使用更多类似…

table.setDefaultRenderer(Object.class, new CustomCellRenderer());

注册单元格渲染器,因为它是TableModel#getColumnClass返回的Class类型,它确定使用哪个单元格渲染器;)

标签:swing,jtable,jlabel,tablecellrenderer,java
来源: https://codeday.me/bug/20191028/1953729.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有