简体   繁体   中英

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. But trying to do same things in text editor and it is running perfectly .

This is the Java code related to my problem:

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

Please help me.

If you are using netbeans then there will be a line in your main() method. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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