When setting the limits of these 3 panels, make sure that
- set the GridBagConstraint
weighty property for something greater than 0, like 1.0, - and set the
anchor property to NORTH or NORTHEAST or NORTHWEST .
Of course, the fill property can only be set to NONE or HORIZONTAL , otherwise all panels will be stretched vertically, which, I think, you do not need.
Here is an example of what I am describing. I simplified your case by replacing 3 large panels with 3 buttons (one of them is higher than the other):
Results (see how 3 buttons are aligned on top):

import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class TestLayout { protected void initUI() { final JFrame frame = new JFrame(TestLayout.class.getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel btmPanel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.weighty = 1.0; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(5, 5, 5, 5); gbc.anchor = GridBagConstraints.NORTH; JButton comp = new JButton("Panel-1"); btmPanel.add(comp, gbc); JButton comp2 = new JButton("Panel-2"); btmPanel.add(comp2, gbc); JButton comp3 = new JButton("Panel-3"); comp3.setPreferredSize(new Dimension(comp.getPreferredSize().width, comp.getPreferredSize().height + 10)); btmPanel.add(comp3, gbc); frame.add(btmPanel); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new TestLayout().initUI(); } }); } }
source share