How to change the background of the JTable header without removing the border?

3

I'm trying to change the background color of header of JTable .

With this code I was able to:

public Principal() throws UnsupportedLookAndFeelException {
        initComponents();

        jTable.getTableHeader().setDefaultRenderer(new HeaderColor());

    }

static public class HeaderColor extends DefaultTableCellRenderer {

    public HeaderColor() {
        setOpaque(true);
    }

    public Component getTableCellRendererComponent(JTable jTable, Object value, boolean selected, boolean focused, int row, int column) {
        super.getTableCellRendererComponent(jTable, value, selected, focused, row, column);
        setBackground(new java.awt.Color(255,255,255));
        return this;
    }
}

This is the result you expected, however, without the border in the header. I need this border.

Is there any way to change this code to insert the border?

    
asked by anonymous 04.11.2016 / 16:16

1 answer

3

Try to put this:

    @Override
    public Component getTableCellRendererComponent(JTable jTable, Object value, boolean selected, boolean focused, int row, int column) {
        super.getTableCellRendererComponent(jTable, value, selected, focused, row, column);
        setBackground(Color.white);
        setBorder(BorderFactory.createLineBorder(Color.black));
        return this;
    }

This is possible because DefaultTableCellRenderer is subclass of JLabel (documentation) . Therefore, what counts to change the border of a JLabel should also be valid for its CellRenderer .

More information on changing borders here .

    
04.11.2016 / 16:23