How to change the Jtable header background without removing the edge?

Asked

Viewed 571 times

3

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

With this code I got:

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 I expected, but it is without the header border. I need that border.

imagem da JTable sem a borda no header

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

  • Ta using Nimbus theme?

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). So what is it worth to change the edge of a JLabel should also apply to your CellRenderer.

More information about changing edges here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.