Question

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.

Was it helpful?

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top