Similar but not identical questions that help: Java - Can I place a JLayeredPane inside a JScrollPane? and How to display animation in a JTable cell and Swing GUI Design using JScrollPane and JLayeredPane .
To do this correctly, there are three separate issues to consider: Resizing, headers, and user interfaces.
Calibration
To scroll and draw correctly, JScrollPane must know the size and preferred size of the component inside it, in this case JLayeredPane . But you want the size to be set by the table, as other Component will float on top of the table. In this case, the easiest way is to create properties related to the JLayeredPane delegate size with JTable as follows.
final JTable table = new JTable(); JLayeredPane layers = new JLayeredPane() { @Override public Dimension getPreferredSize() { return table.getPreferredSize(); } @Override public void setSize(int width, int height) { super.setSize(width, height); table.setSize(width, height); } @Override public void setSize(Dimension d) { super.setSize(d); table.setSize(d); } };
If you do not want JTable to JTable a thing determining the size of a JLayeredPane , then you need to determine it in some other way, as well as the size of the table. Both will have to setPreferredSize() and setSize() explicitly call them.
Headings
Since JTable is no longer a viewport, you need to link the headers yourself. The following code will work:
scrolling.setColumnHeaderView(table.getTableHeader()); scrolling.setRowHeaderView(rowHeader);
User interface
Also note that JTable does some nasty codes in configureEnclosingScrollPane() and configureEnclosingScrollPaneUI() . If you want the user interface style changes to work correctly, you will have to override these methods, but I have not yet developed how to do this.
source share