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());
Try taking the defaults from
UIManager
:There are more issues involved then just getting the color and border of the table header. Each cell/column is rendered by a
TableCellRenderer
meaning that the values return by theUIManager
may be ignored...For example, the following renders the
JTableHeader
and applies border/background to aJLabel
based on values returned by theUIManager
under 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
JTableHeader
to the scroll panes column view directly...UPDATED
You need to set a look and feel for the application before trying:
you should set a look and feel first like so:
Here is an example:
I figured I'll create a
JTable
without any rows and place aJTextPane
right underneath. And it works like charm.