Search code examples
javaswingjpaneljscrollpane

How to get JScrollPanes within a JScrollPane to follow parent's resizing


So I have a bunch of JTables. Each JTable is inside a JScrollPane. I'm then adding each of these JScrollPanes to a JPanel. I'm then adding this JPanel to a JScrollPane then that to another JPanel with BorderLayout. The larger JScrollPane properly resizes with its parent, but each of the smaller JScrollPanes have constant height, which is larger than the window when it is small. How can I get each of the children JScrollPanes to resize with the height of the window/their parent?

I've tried adding more intermediary JPanels with FlowLayout, BorderLayout, and nothing seems to work.

Here's some relevant code:

public class MyPanel extends JPanel
{
    public MyPanel()
    {
        super(new BorderLayout());
        JPanel panel = new JPanel();
        for (int i = 0; i < 5; i++)
        {
            // View extends JTable
            panel.add(new JScrollPane(new View(new Model())));
        }
        add(new JScrollPane(panel));
    }
}

I'm really just trying to get a bunch of tables with scrollbars horizontally next to each other inside a larger panel with a horizontal scrollbar. And I want all that stuff to resize appropriately when the window size changes.

more code:

final MyPanel panel = new MyPanel();
final JTabbedPane tabView = new JTabbedPane();
tabView.add(...);
tabView.add("foo", panel);

final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, ..., tabView);
this.add(splitPane); // this extends JFrame

Solution

  • You can use a BoxLayout. If you want the opposite: some table being fixed, you can wrap it with constraint Box.createRigidArea(new Dimension(100, 100)) .