简体   繁体   English

如何在Netbeans中更改JTable标题单元格的颜色

[英]How do I change JTable title cell color in Netbeans

I tried to change JTable title cells colour in NetBeans but it doesn't change. 我试图更改NetBeans中JTable标题单元格的颜色,但没有改变。 But trying to do same things in text editor and it is running perfectly . 但是尝试在文本编辑器中做同样的事情,它运行完美。

This is the Java code related to my problem: 这是与我的问题有关的Java代码:

jTable1.getTableHeader().setBackground(Color.GREEN);

Please help me. 请帮我。

If you are using netbeans then there will be a line in your main() method. 如果您使用的是netbeansmain()方法中会有一行。 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

Comment this line and then see the result. 注释此行,然后查看结果。

The problem is Netbeans gives a set up look and feel. 问题是Netbeans提供了设置外观。 You can create custom renderer though like this 您可以像这样创建自定义渲染器

public NewJFrame() {
    initComponents();

    jTable1.getTableHeader().setDefaultRenderer(new 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);

            l.setBorder(new LineBorder(Color.black, 1));

            l.setBackground(Color.GREEN);

            return l;
        }
    });
}

在此处输入图片说明

Also made with GUI Builder 也使用GUI Builder制作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM