1

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
Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
  • Are you sure it's the JScrollPane that's not resizing? It could be the parent JPanel. Try `add(new JScrollPane(panel), BorderLayout.CENTER);`, should work. Also for troubleshooting Swing components, setting a background color tends to help find the problematic component. –  Feb 26 '12 at 22:40
  • That didn't change anything. I believe the default of the BorderLayout is BorderLayout.CENTER. – Andrew Rasmussen Feb 26 '12 at 22:42
  • 1
    The best way of getting a decent quick answer is by creating and posting a decent [sscce](http://sscce.org). – Hovercraft Full Of Eels Feb 26 '12 at 22:45
  • Can you post the code where you add a MyPanel to a JFrame? –  Feb 26 '12 at 22:45
  • Posted. @HovercraftFullOfEels believe me this is the short version. – Andrew Rasmussen Feb 26 '12 at 22:50
  • 1
    @arasmussen: but it's neither compilable nor runnable. If you don't get a decent answer soon, you may want to read the link to see the details. Again it's [sscce](http://sscce.org). – Hovercraft Full Of Eels Feb 26 '12 at 22:52
  • I don't see how I could post a compileable example of JTables inside JScrollPanes inside a JPanel inside a JScrollPane inside a JPanel and keep it short at the same time. – Andrew Rasmussen Feb 26 '12 at 22:53
  • You could if the JTables all use the same dummy data. No one is requiring you to do this of course, so it's up to you, but my experience here is that it helps much if we can see the code, run the code, experience the problem, manipulate the code, and solve the problem. The longer the program, the harder it is for us to quickly understand it of course. – Hovercraft Full Of Eels Feb 26 '12 at 22:55

1 Answers1

2

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)) .

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138