Search code examples
javaswinggridbaglayout

Fix the last element of a GridBagLayout to the bottom of the page


I have a GridBagLayout. The last element (startButton) jumps up and down depending on how much text there is in the above text box. Is there any way of fixing its position? I tried changing PAGE_START to PAGE_END, but that didn't do anything.

contentPanel.add(titleText, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.PAGE_START,
                GridBagConstraints.HORIZONTAL, new Insets(50, 100, 0, 100), 0, 0));
        contentPanel.add(expText, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.PAGE_START,
                GridBagConstraints.CENTER, new Insets(120, 100, 00, 100), 0, 0));
        contentPanel.add(buttonsPanel, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.PAGE_START,
                GridBagConstraints.HORIZONTAL, new Insets(120, 100, 0, 100), 0, 0));

        contentPanel.add(userText, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.PAGE_START,
                GridBagConstraints.CENTER, new Insets(120, 0, 0, 0), 0, 0));
        contentPanel.add(startButton, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.PAGE_START,
                GridBagConstraints.CENTER, new Insets(180, 0, 0, 0), 0, 0));

        contentPanel.add(Box.createVerticalGlue(), new GridBagConstraints(0, 1, 2, 2, 1, 1,
                GridBagConstraints.PAGE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

Solution

  • I was able to do it like this:

    contentPanel.add(startButton, new GridBagConstraints(0, 3, 1, 1, 1, 0, GridBagConstraints.PAGE_START,
                    GridBagConstraints.CENTER, new Insets(-180, 0, 0, 0), 0, 0));