Search code examples
javaswingjscrollpanelayout-managergridbaglayout

Incorrect GridBagLayout behaviour


When I resize JFrame, the table (or more specifically the scrollPane) squishes into a little rectangle (with remains the same, but height is more like 5-10 pixels).

Also I noticed that there's a specific height threshold of JFrame, and when height of JFrame drops below this, all GridBagLayouts behave correctly (e.g. correct width & height of all panels). When the height is higher than this threshold, both right panels gain additional width and the rightTopPanel loses its height much faster than the rightBottomPanel when decreasing height of JFrame.

The threshold seems to be point when rightTopPanel gets minimum height (about 5-10 pixels).

JPanel panel = new JPanel(new GridBagLayout());

JPanel rightTopPanel = new JPanel(new GridBagLayout());
panel.add(rightTopPanel, Helper.createGridBagConstraints(1, 0, 1, 1, 0.5, 0.5, GridBagConstraints.BOTH));

JPanel rightBottomPanel = new JPanel(new GridLayout(1, 1));

tableModel = new DefaultTableModel(0, 2);
JTable table = new JTable(tableModel);

JScrollPane scrollPane = new JScrollPane(table);
rightBottomPanel.add(scrollPane);

panel.add(rightBottomPanel, Helper.createGridBagConstraints(1, 1, 1, 1, 0.5, 0.5, GridBagConstraints.BOTH));

JPanel leftPanel = new JPanel();
panel.add(leftPanel, Helper.createGridBagConstraints(0, 0, 1, 2, 0.5, 1.0));

add(panel);

Helper.createGridBagConstraints is just a helper method to create GridBagConstraints, with numerous "optional" parameters.

GridBagConstraints createGridBagConstraints(int gridx, int gridy)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill)

Edit: It seems that this is a problem within JScrollPane. If I don't add it, remaining panels resize correctly.

Edit 2: Since, so far no one understands what's my problem, here are screenshots: http://img155.imageshack.us/img155/8847/badgridbaglayout1.png

http://img17.imageshack.us/img17/8779/badgridbaglayout2.png

http://img28.imageshack.us/img28/9336/badgridbaglayout3.png


Solution

  • The only solution is to NOT use any layout at all and position/resize components manually, because none of the builtin layouts in Swing are implemented properly.