2

GridBagConstraints aren't working even with .weightx, .weighty, and .anchor specified. I need the component to be set in the top left but it keeps being set in the center of the screen. Below is the method I have for positioning the component.

private void initGUI(){
    getContentPane().setLayout(new GridBagLayout());

    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(450, 450));

    Box buttonBox = Box.createVerticalBox();
    ButtonGroup buttons = new ButtonGroup();

    buttons.add(okOnly);
    buttons.add(okCancel);
    buttons.add(yesNoCancel);
    buttons.add(yesNo);

    buttonBox.add(okOnly);
    buttonBox.add(okCancel);
    buttonBox.add(yesNoCancel);
    buttonBox.add(yesNo);
    buttonBox.setBorder(BorderFactory.createTitledBorder("Buttons"));

    GridBagConstraints gridConstraints = new GridBagConstraints();
    gridConstraints.gridx = 0;
    gridConstraints.gridy = 0;
    gridConstraints.anchor = GridBagConstraints.NORTHWEST;
    gridConstraints.weightx = 1;
    gridConstraints.weighty = 1;

    panel.add(buttonBox, gridConstraints);
    getContentPane().add(panel);
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
programmer
  • 71
  • 8
  • 1
    Been a while since I used GridBag stuff, so this may be a stupid comment, but: Your GridBagLayout is on the content pane but your pass the constraints when you add the buttonBox to a different panel. Are you sure all that is right? – John3136 Oct 03 '16 at 21:34
  • @John3136: no, it's not alright. See code below. – Hovercraft Full Of Eels Oct 03 '16 at 22:46
  • Yeah, the problem was there was no layout manager for the panel, which i was adding too. Thanks for the catch. – programmer Oct 04 '16 at 00:04

1 Answers1

4

You're giving the GridBagLayout to the contentPane, but adding a component to it without GridBagConstraints, and then giving no layout to your panel JPanel, so that it uses the default FlowLayout, and adding components to it using GridBagConstraints, constraints that have no meaning in this situation.

Solution: obviously don't do this. Only use GridBagConstraints when adding components to a container that uses GridBagLayout.

In fact, I'd simply get rid of the panel JPanel if all you need is a collection of JRadioButtons in the upper left corner:

import java.awt.*;
import javax.swing.*;

public class Gui extends JFrame {
    private JRadioButton okOnly = new JRadioButton("Ok ");
    private JRadioButton okCancel = new JRadioButton("Ok Cancel");
    private JRadioButton yesNoCancel = new JRadioButton("Yes No Cancel");
    private JRadioButton yesNo = new JRadioButton("Yes No");

    private void initGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setLayout(new GridBagLayout());

        // JPanel panel = new JPanel();
        // panel.setPreferredSize(new Dimension(450, 450));

        setPreferredSize(new Dimension(450, 450));

        Box buttonBox = Box.createVerticalBox();
        ButtonGroup buttons = new ButtonGroup();

        buttons.add(okOnly);
        buttons.add(okCancel);
        buttons.add(yesNoCancel);
        buttons.add(yesNo);

        buttonBox.add(okOnly);
        buttonBox.add(okCancel);
        buttonBox.add(yesNoCancel);
        buttonBox.add(yesNo);
        buttonBox.setBorder(BorderFactory.createTitledBorder("Buttons"));

        GridBagConstraints gridConstraints = new GridBagConstraints();
        gridConstraints.gridx = 0;
        gridConstraints.gridy = 0;
        gridConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridConstraints.weightx = 1;
        gridConstraints.weighty = 1;

        // panel.add(buttonBox, gridConstraints);
        add(buttonBox, gridConstraints);
        // getContentPane().add(panel);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private static void createAndShowGui() {
        new Gui().initGUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373