user1734945
user1734945

Reputation: 79

Multiple JPanel GridBagConstraints alignment

enter image description here

As shown in the pic, firstly there are two panels: topPanel and btmPanel. The topPanel consists of another two panel, one filled with black and one filled with gray this is not the problem.

In the btmPanel there are three panels which are all in GridbagLayouts, each of the panels have different amount of JButtons The problem is the third panel have more JButtons so what I want is to align them to start from the top. Is that possible?

Thanks.

Upvotes: 4

Views: 959

Answers (2)

Guillaume Polet
Guillaume Polet

Reputation: 47608

When setting the constraints of those 3 panels, make sure to

  1. set the GridBagConstraint property weighty to something bigger than 0, for example 1.0,
  2. and also set the property anchor 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 guess, you don't want.

Here is an example of what I describe. I simplified your case by replacing the 3 big panels with 3 buttons (one of them is taller than the other):

Results (see how 3 buttons are aligned on top):

enter image description here

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();
            }
        });
    }
}

Upvotes: 2

nIcE cOw
nIcE cOw

Reputation: 24626

Simply set the Layout for the btmPanel to GridLayout(1, 3, 5, 5);, that will align them to the top. Since presently the default Layout for the btmPanel is FlowLayout, hence you experiencing this issue. On top of this btmPanel you have these three JPanels with GridBagLayout.

Upvotes: 1

Related Questions