向 JTable 列添加文本背景颜色

发布于 2024-10-08 15:41:33 字数 120 浏览 0 评论 0原文

动机是我想看到表格单元格中的尾随空格。例如,如果单元格包含“Foo Bar”,我希望在“Bar”之后看到空格字符。有没有办法更改文本背景颜色,以便我可以轻松地在 JTable 单元格中看到所有字符?我希望在整个专栏中都这样做。

The motivation is that I want to see trailing spaces in table cells. For instance, if the cell contains "Foo Bar ", I would like to see the space character after "Bar". Is there a way to change the text background color so that I can see all the characters easily in a JTable cell? I'm looking to do this for a whole column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

且行且努力 2024-10-15 15:41:33

尝试修改第 i 列的单元格 defaulttablecellrenderer 中的组件。
我认为 arg4 和 arg5 是表格的行和列,因此您也可以控制此处单元格的渲染器。

    JTable table = new JTable();
    table.getColumn(i).setCellRenderer(new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable arg0, Object arg1,
                boolean arg2, boolean arg3, int arg4, int arg5) {
            Component component = super.getTableCellRendererComponent(arg0, arg1, arg2, arg3, arg4, arg5);
            // modify this component here
            // component.setForeground(Color.black.black);
            // component.setBackground(Color.black.black);
            return component;
        }
    });

try modifying the component in the cell defaulttablecellrenderer for column i.
I think arg4 and arg5 are row and column of the table so you can control the renderer for the cells here too.

    JTable table = new JTable();
    table.getColumn(i).setCellRenderer(new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable arg0, Object arg1,
                boolean arg2, boolean arg3, int arg4, int arg5) {
            Component component = super.getTableCellRendererComponent(arg0, arg1, arg2, arg3, arg4, arg5);
            // modify this component here
            // component.setForeground(Color.black.black);
            // component.setBackground(Color.black.black);
            return component;
        }
    });
梦途 2024-10-15 15:41:33
    final JTable table = new JTable(tableModel);
    table.setPreferredScrollableViewportSize(TABLE_SIZE);
    table.setFillsViewportHeight(true);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    table.getTableHeader().addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent mouseEvent) {
            int selectedHeader = table.convertColumnIndexToModel(table
                    .columnAtPoint(mouseEvent.getPoint()));

            table.getColumn(table.getColumnName(selectedHeader))
                    .setCellRenderer(new DefaultTableCellRenderer() {
                        public void setBackground(Color c) {
                            super.setBackground(Color.blue);
                        }
                    });
        };
    });
    final JTable table = new JTable(tableModel);
    table.setPreferredScrollableViewportSize(TABLE_SIZE);
    table.setFillsViewportHeight(true);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    table.getTableHeader().addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent mouseEvent) {
            int selectedHeader = table.convertColumnIndexToModel(table
                    .columnAtPoint(mouseEvent.getPoint()));

            table.getColumn(table.getColumnName(selectedHeader))
                    .setCellRenderer(new DefaultTableCellRenderer() {
                        public void setBackground(Color c) {
                            super.setBackground(Color.blue);
                        }
                    });
        };
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文