Search code examples
javajbuttongridbaglayout

Buttons wont resize correctly when using a GridBagLayout


I am trying to use Java's GridBagLayout to create a form that contains 6 buttons.

The layout is supposed to look kind of like this: https://i.sstatic.net/Speln.jpg

So its supposed to be two large buttons on top with 4 smaller ones underneath but all equal sizes. As you can see from the image above they are not all the same size. How would i fix this?

This is the code i have to lay it out:

public void InitialiseComponents() {
    btnSouthWestBlockManagement = new JButton();
    btnDevonBlockManagement = new JButton();
    btnAccounts = new JButton();
    btnContacts = new JButton();
    btnIssuesRegister = new JButton();
    btnMaintainance = new JButton();

    btnDevonBlockManagement.setText("Devon Block Management");
    btnSouthWestBlockManagement.setText("South West Block Management");
    btnAccounts.setText("Accounts");
    btnContacts.setText("Contacts");
    btnIssuesRegister.setText("Issues Register");
    btnMaintainance.setText("Maintainance");

    this.add(btnDevonBlockManagement, sizeComponent(0, 0, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.5, 0.5));
    this.add(btnSouthWestBlockManagement, sizeComponent(2, 0, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.5, 0.5));
    this.add(btnAccounts, sizeComponent(0, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.25, 0.5));
    this.add(btnContacts, sizeComponent(1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.25, 0.5));
    this.add(btnIssuesRegister, sizeComponent(2, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.25, 0.5));
    this.add(btnMaintainance, sizeComponent(3, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.25, 0.5));

}

protected GridBagConstraints sizeComponent(int gridx, int gridy, int gridwidth, int gridheight, int fill, int anchor, double weightx, double weighty) {
    GridBagConstraints gridBagConstraints;
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = gridx;
    gridBagConstraints.gridy = gridy;
    gridBagConstraints.gridwidth = gridwidth;
    gridBagConstraints.gridheight = gridheight;
    gridBagConstraints.fill = fill;
    gridBagConstraints.anchor = anchor;
    gridBagConstraints.weightx = weightx;
    gridBagConstraints.weighty = weighty;
    return gridBagConstraints;
}

Solution

  • I managed to fix this by adding struts in the top column to keep all the components the same size. the code i used was

        this.add(Box.createHorizontalStrut(100), sizeComponent(0, 0, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0, 0));
        this.add(Box.createHorizontalStrut(100), sizeComponent(1, 0, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0, 0));
        this.add(Box.createHorizontalStrut(100), sizeComponent(2, 0, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0, 0));
        this.add(Box.createHorizontalStrut(100), sizeComponent(3, 0, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0, 0));