I’m trying to set up my panels in my program, and I’m not getting what I think is the expected behaviour of my GridBagLayout (of course it is correct, GIGO). I’d like some of the components on the page to resize.
The way I have it set up is in two parts:
1. The Content Pane, inside the Manager Pane:
myPlayerContentPane =
new PlayerContentPane(myEventListener);
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty =1;
this.add(myPlayerContentPane,c);
myPlayerContentPane.setBorder(BorderFactory.createLineBorder(Color.black));
This sub-pane should expand as I resize my frame, correct? This is because of the weight and fill commands.
2. The contents of the content pane:
setLayout(new GridBagLayout());
c.insets = new Insets(1, 1, 1, 1);
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
this.add(newCharacterButton,c);
newCharacterButton.addActionListener(new ActionListener(){
...
});
c.gridx = 1;
c.gridy = 0;
c.gridheight = 1;
c.fill = GridBagConstraints.HORIZONTAL;
this.add(newCharacterName,c);
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
this.add(loadCharacterButton,c);
loadCharacterButton.addActionListener(new ActionListener() {
...
});
c.gridx = 2;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
this.add(editCharacterButton,c);
c.gridx = 2;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
this.add(deleteCharacterButton,c);
deleteCharacterButton.addActionListener(new ActionListener(){
...
});
c.gridx = 2;
c.gridy = 2;
c.fill = GridBagConstraints.BOTH;
c.gridheight = 3;
c.ipady = 200;
c.weighty = 1;
this.add(characterListScroller,c);
c.gridx = 0;
c.gridy = 4;
c.fill = GridBagConstraints.BOTH;
c.gridwidth = 2;
c.ipady = 200;
c.weighty =1;
this.add(characterSummaryScroller,c);
Now, when the program loads it looks fine, and I expect on resizing that the two JScrollPanes would resize. However, even when I’ve removed all instances of setting a preferred size in my code (even for the frame itself) I don’t see any resizing behaviour.
Here is the setup when the program first loads, snapped to the minimum size:

And here is when I’ve resized the frame, and the panels don’t follow suit.

Now my Manager pane is inside a frame with a CardLayout, and I’ve just checked and that doesn’t resize with the frame. And that’s the top level pane (that I set up). How can I make it all resize?
If you wrap your top panel inside another JPanel with a BorderLayout (assuming you put the panel in the CENTER), you should get the desired behaviour. BorderLayout automatically adjust to the frame size and force its center panel to take the entire available area.