开发者

Add text background color to JTable column

The motivation is 开发者_StackOverflowthat 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.


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;
        }
    });


    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);
                        }
                    });
        };
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜