如何设置JLabel的背景和边框与表格标题相同? [英] How can I set a JLabel's background and border the same as a table header?

查看:52
本文介绍了如何设置JLabel的背景和边框与表格标题相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JLabel重新创建表头外观. JLabel的外观必须与系统指定的JTableHeader完全相同.

I want to recreate a table header looks using JLabel. The look and feel of the JLabel needs to be exactly like the JTableHeader would be, specified by the system.

这是我到目前为止尝试过的:

This is what I have tried so far:

JLabel header = new JLabel("Title");
header.setOpaque(true);
header.setBackground(UIManager.getColor(new JTableHeader().getBackground()));
header.setBorder(UIManager.getBorder(new JTableHeader().getBorder()));

但是,UIManager会返回null的颜色和边框.

But, the UIManager returns null for the color and border.

有什么想法吗?

这是我设置外观的方式:

This is how I set the Look and Feel:

javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());

推荐答案

涉及更多的问题,而不仅仅是获取表标题的颜色和边框.每个单元格/列均由TableCellRenderer表示,这意味着UIManager返回的值可能会被忽略...

There are more issues involved then just getting the color and border of the table header. Each cell/column is rendered by a TableCellRenderer meaning that the values return by the UIManager may be ignored...

例如,以下内容根据窗口下方外观下UIManager返回的值,渲染JTableHeader并将边框/背景应用于JLabel.

For example, the following renders the JTableHeader and applies border/background to a JLabel based on values returned by the UIManager under the Window's Look and Feel...

如您所见,它们之间有很大的区别

As you can see, there's quite a difference between them

但是,如果您只想在滚动窗格的另一个组件的顶部显示某种组头",则可以直接在滚动窗格的列视图中添加JTableHeader. ..

How ever, if all you're interested in is display a "group header" of some kind over the top of another component on a scroll pane, you could simply add a JTableHeader to the scroll panes column view directly...

public class TestHeader {

    public static void main(String[] args) {
        new TestHeader();
    }

    public TestHeader() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                TableColumnModel model = new DefaultTableColumnModel();
                final TableColumn column = new TableColumn(0, 250);
                column.setHeaderValue("Test");
                model.addColumn(column);

                JTableHeader header = new JTableHeader();
                header.setColumnModel(model);

                final JTextArea textArea = new JTextArea();

                JScrollPane scrollPane = new JScrollPane(textArea);
                scrollPane.setColumnHeaderView(header);

                textArea.addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentResized(ComponentEvent e) {
                        column.setWidth(textArea.getWidth());
                    }
                });

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(scrollPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

已更新

public class TestHeader {

    public static void main(String[] args) {
        new TestHeader();
    }

    public TestHeader() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                TableColumnModel model = new DefaultTableColumnModel();
                final TableColumn column = new TableColumn(0, 250);
                column.setHeaderValue("I don't see the problem");
                model.addColumn(column);

                final JTableHeader header = new JTableHeader();
                header.setColumnModel(model);

                DefaultTableModel tm = new DefaultTableModel(new Object[]{"A", "B", "C"}, 0);
                tm.addRow(new Object[]{"1", "2", "3", "4"});
                tm.addRow(new Object[]{"5", "6", "7", "8"});
                tm.addRow(new Object[]{"9", "10", "11", "12"});
                tm.addRow(new Object[]{"13", "14", "15", "16"});
                final JTable table = new JTable(tm);

                final JScrollPane scrollPane = new JScrollPane(table);
                /**
                 * For some reason, the header isn't being applied as soon as the
                 * table is added to the scroll pane, so we need to jump our next
                 * request to the end of the of event queue so that it will
                 * occur some time in the future
                 */
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        scrollPane.setColumnHeaderView(header);
                    }

                });

                table.addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentResized(ComponentEvent e) {
                        column.setWidth(table.getWidth());
                    }

                });

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(scrollPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

}

这篇关于如何设置JLabel的背景和边框与表格标题相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆