I want to recreate a table header looks using JLabel. The look and feel of the JLabel needs to be exactly like the JTableHeader would be, specified by the system.
This is what I have tried so far:
JLabel header = new JLabel("Title");
header.setOpaque(true);
header.setBackground(UIManager.getColor(new JTableHeader().getBackground()));
header.setBorder(UIManager.getBorder(new JTableHeader().getBorder()));
But, the UIManager returns null for the color and border.
Any ideas?
This is how I set the Look and Feel:
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
There are more issues involved then just getting the color and border of the table header. Each cell/column is rendered by a
TableCellRenderermeaning that the values return by theUIManagermay be ignored…For example, the following renders the
JTableHeaderand applies border/background to aJLabelbased on values returned by theUIManagerunder the Window’s Look and Feel…As you can see, there’s quite a difference between them
How ever, if all you’re interested in is display a “group header” of some kind over the top of another component on a scroll pane, you could simply add a
JTableHeaderto the scroll panes column view directly…UPDATED