JTable inside JLayeredPane inside JScrollPane - how do you make it?

I want the objects coming out of JTable to be on top of it, so using JLayeredPane seems natural. However, in order to draw correctly, whether to create headers correctly, etc., it is very difficult. How to do this to:

  • Line headers display and match when they scroll
  • Column headers display and match when they scroll
  • The table is painted correctly
  • resizing won't spoil everything.

Please note that since JDesktopPane extends JLayeredPane , the answers to this question also allow you to have JTable (or any other component) behind JDesktopPane .

+4
source share
1 answer

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); } }; // NB you must use new Integer() - the int version is a different method layers.add(label, new Integer(JLayeredPane.PALETTE_LAYER), 0); JScrollPane scrolling = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrolling.setViewportView(layers); 

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.

+4
source

All Articles