Reputation: 71
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);
}
Upvotes: 2
Views: 131
Reputation: 285405
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());
}
}
Upvotes: 4