user2351151
user2351151

Reputation: 733

GridBagConstraints for Java

Here is some Java Code that I have that works that uses GridBagConstraints:

public AuctionClient() {
    JFrame guiFrame = new JFrame();
    JPanel guiPanel = new JPanel(new GridBagLayout());
    JLabel userNameLabel = new JLabel("UserName:");
    JTextField userNameTextField = new JTextField(30);
    JButton loginButton = new JButton("Login");
    JButton registerButton = new JButton("Register");                

    JLabel passwordLabel = new JLabel("Password:");
    JTextField passwordTextField = new JPasswordField(30);

    guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    guiFrame.setTitle("Auction Client");
    guiFrame.setSize(500, 250);

    guiFrame.setLocationRelativeTo(null);
    GridBagConstraints labelGBC = new GridBagConstraints();
    labelGBC.insets = new Insets(3, 3, 3, 3);
    GridBagConstraints fieldGBC = new GridBagConstraints();
    fieldGBC.insets = new Insets(3, 3, 3, 3);
    fieldGBC.gridwidth = GridBagConstraints.REMAINDER;
    guiPanel.add(userNameLabel, labelGBC);
    guiPanel.add(userNameTextField, fieldGBC);

    guiPanel.add(passwordLabel, labelGBC);
    guiPanel.add(passwordTextField, fieldGBC);    

    GridBagConstraints loginButtonGBC = new GridBagConstraints();
    loginButtonGBC.insets = new Insets(3, 3, 3, 3);        
    GridBagConstraints registerButtonGBC = new GridBagConstraints();
    registerButtonGBC.insets = new Insets(3, 3, 3, 3);        
    registerButtonGBC.gridwidth = GridBagConstraints.REMAINDER;
    guiPanel.add(loginButton, loginButtonGBC);                        
    guiPanel.add(registerButton, registerButtonGBC);                                

    guiFrame.add(guiPanel, BorderLayout.NORTH);
    guiFrame.setVisible(true);
}

I have had a look online for some explanation of how the GridBagConstraints work in relation to placing controls on a panel. I could not understand exactly how it works so am asking a question here on this forum.

Here is a screenshot of the above code when running:

enter image description here

Can I please have some help to position the Login and Register buttons in the middle of the panel, side by side.

EDIT

Here is my current working code:

    public AuctionClientLogon() {
    JFrame guiFrame = new JFrame();
    JPanel guiFieldsPanel = new JPanel(new GridBagLayout());
    JPanel guiButtonsPanel = new JPanel(new GridBagLayout());        
    JLabel userNameLabel = new JLabel("UserName:");
    JTextField userNameTextField = new JTextField(30);
    JButton loginButton = new JButton("Login");
    JButton registerButton = new JButton("Register");                

    JLabel passwordLabel = new JLabel("Password:");
    JTextField passwordTextField = new JPasswordField(30);

    guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    guiFrame.setTitle("Auction Client");
    guiFrame.setSize(500, 250);

    guiFrame.setLocationRelativeTo(null);

    GridBagConstraints gbc = new GridBagConstraints();        
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.CENTER;        

    guiFieldsPanel.add(userNameLabel, gbc);
    gbc.gridx++;
    guiFieldsPanel.add(userNameTextField, gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;        
    guiFieldsPanel.add(passwordLabel, gbc);
    gbc.gridx++;        
    guiFieldsPanel.add(passwordTextField, gbc);    

    gbc.anchor = GridBagConstraints.CENTER;                
    gbc.gridx = 0;
    gbc.gridy = 1;
    guiButtonsPanel.add(loginButton, gbc);     
    gbc.gridx++;                
    guiButtonsPanel.add(registerButton, gbc);                                

    guiFrame.add(guiFieldsPanel, BorderLayout.NORTH);
    guiFrame.add(guiButtonsPanel, BorderLayout.CENTER);        
    guiFrame.setVisible(true);
}

Here is an image:

http://canning.co.nz/Java/Positioning_Image2.png

Is it possible to place the Labels and TextFields in the Center as well as the buttons, but not on top of each other? I would like everything in the Center if possible, but the buttons to be a little bit lower than the Labels and TextFields. Is this possible?

Upvotes: 1

Views: 3870

Answers (2)

Sanjaya Liyanage
Sanjaya Liyanage

Reputation: 4746

Use a FlowLayout to keep this kind of line of buttons.Then you can easily place the button position where ever you want inline. Have a look at how to use flow layout

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347244

Start by taking a look at your forms requirements. You have two sections. The fields and the buttons. Each of these sections have a (slightly) different layout requirement.

Start by separating the layout to best meet these requirements.

Create a JPanel to hold the fields and a JPanel for the buttons. These panels could have different layouts if required, but the example I've included uses GridBagLayout for each.

Then layout your components accordingly (on there individual panels).

Then bring it all together...

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class GridBagLayout01 {

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

    public GridBagLayout01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame guiFrame = new JFrame();
                JPanel guiPanel = new JPanel(new GridBagLayout());
                JLabel userNameLabel = new JLabel("UserName:");
                JTextField userNameTextField = new JTextField(30);
                JButton loginButton = new JButton("Login");
                JButton registerButton = new JButton("Register");

                JLabel passwordLabel = new JLabel("Password:");
                JTextField passwordTextField = new JPasswordField(30);

                guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                guiFrame.setTitle("Auction Client");
                guiFrame.setSize(500, 250);

                guiFrame.setLocationRelativeTo(null);

                JPanel fields = new JPanel(new GridBagLayout());

                GridBagConstraints labelGBC = new GridBagConstraints();
                labelGBC.insets = new Insets(3, 3, 3, 3);
                GridBagConstraints fieldGBC = new GridBagConstraints();
                fieldGBC.insets = new Insets(3, 3, 3, 3);
                fieldGBC.gridwidth = GridBagConstraints.REMAINDER;
                fields.add(userNameLabel, labelGBC);
                fields.add(userNameTextField, fieldGBC);

                fields.add(passwordLabel, labelGBC);
                fields.add(passwordTextField, fieldGBC);

                JPanel buttons = new JPanel(new GridBagLayout());

                GridBagConstraints loginButtonGBC = new GridBagConstraints();
                loginButtonGBC.insets = new Insets(3, 3, 3, 3);
                GridBagConstraints registerButtonGBC = new GridBagConstraints();
                registerButtonGBC.insets = new Insets(3, 3, 3, 3);
                registerButtonGBC.gridwidth = GridBagConstraints.REMAINDER;
                buttons.add(loginButton, loginButtonGBC);
                buttons.add(registerButton, registerButtonGBC);

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                guiPanel.add(fields, gbc);
                guiPanel.add(buttons, gbc);

                guiFrame.add(guiPanel, BorderLayout.NORTH);
                guiFrame.setVisible(true);
            }
        });
    }
}

Upvotes: 1

Related Questions