Search code examples
javaswingnetbeansjtablejtableheader

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.


Solution

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

    enter image description here

    Also made with GUI Builder