Dragon
Dragon

Reputation: 2481

GridBagLayout is in the center of BoxLayout instead of being on the top

The sample below describes the following app:

Button that is on the NORTH of the BorderLayout, adds GridBagLayout panel with some components to the BoxLayout.Y_AXIS that is inside BorderLayout.CENTER. But after clicking the button, panel appears in the center, instead of being added to the top.

public class Test extends JFrame {
    public Test() {
        super("Test");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        JButton add = new JButton("Add");
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.add(new RecordPanel("text1", "text2"));
                panel.revalidate();
            }
        });

        Container container = getContentPane();
        container.add(add, BorderLayout.PAGE_START);
        container.add(panel, BorderLayout.CENTER);

        setSize(300, 500);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Test();
    }

    private class RecordPanel extends JPanel {
        private JRadioButton radioButton;
        private JLabel textLabel1;
        private JLabel textLabel2;

        public RecordPanel(String text1, String text2) {
            super();
            radioButton = new JRadioButton();
            textLabel1 = new JLabel(text1);
            textLabel2 = new JLabel(text2);
            initGUI();
        }

        private void initGUI() {
            setLayout(new GridBagLayout());
            GridBagConstraints constraints = new GridBagConstraints();

            constraints.gridx = 0;
            constraints.gridy = 0;
            add(radioButton, constraints);

            constraints.gridx = 1;
            constraints.gridy = 0;
            constraints.weightx = 1.0;
            constraints.fill = GridBagConstraints.HORIZONTAL;
            add(textLabel1, constraints);

            constraints.gridx = 1;
            constraints.gridy = 1;
            constraints.weightx = 1.0;
            constraints.fill = GridBagConstraints.HORIZONTAL;
            add(textLabel2, constraints);
        }
    }
}

Playing with anchors (constraints.anchor = GridBagConstraints.FIRST_LINE_START) set radio button and one label to the top, but second label still appears in the center.

How to make panels appear on the top instead of center?

Upvotes: 0

Views: 452

Answers (3)

Reimeus
Reimeus

Reputation: 159754

The problem is that the maximum size is by default defined as Integer.MAX_VALUE x Integer.MAX_VALUE for the newly added RecordPanel component causing the maximum available area to be used. This dimension is observed by BoxLayout.

You could override getMaximumSize in the class RecordPanel

@Override
public Dimension getMaximumSize() {
   return new Dimension(300, getPreferredSize().height);
}

The documentation has full details on how sizing is achieved using BoxLayout

Upvotes: 1

Math
Math

Reputation: 3396

If you don't mind to use MigLayout instead of BoxLayout this might be a solution for you:

First, change your panel layout to MigLayout:

panel.setLayout(new MigLayout("", "[]", "[]"));

Create an static variable for the class to count in which row the new `RecordPanel' element is added to:

public class Test extends JFrame {
    static int index = 0;

Then replace the add to the panel line to:

panel.add(new RecordPanel("text1", "text2"), "cell 0 " + index++);

This will insert every new RecordPanel into a new row, thanks to the index always being incremented.

You may want to wrap your panel into a ScrollView to add a scroll bar when the elements fill the panel.

Upvotes: 0

M.K.
M.K.

Reputation: 276

The element GridBagConstraints.anchor is the right choice but you have to use one of these values GridBagConstraints.NORTH to GridBagConstraints.NORTHWEST.

For example constraints.anchor = GridBagConstraints.NORTH

Upvotes: 0

Related Questions