GridBagLayout manager is one of the most powerful but complex layout manager in java.It is somewhat similar to
component added can be of varying sizes i.e we can increase or decrease their size to occupy more than one row or column of the grid.
If suppose you are going to use this layout manager in your program then you first decide and draw the outline of GUI you need then proceed further.Life will be much easier(Really).
.They specify the size and position, and many more constraints of the each component. We will discuss about each
” instance which is used for specifying the constraints of all the components or seperate “
” for each of them.
for each of the component added to the container using GridBagLayout Manager.
package com.techy.rajeev;
import java.awt.EventQueue;
public class GridBagLayoutTest {
private JFrame frmTechyrajeev;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GridBagLayoutTest window = new GridBagLayoutTest();
window.frmTechyrajeev.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GridBagLayoutTest() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmTechyrajeev = new JFrame();
frmTechyrajeev.setTitle("techyrajeev");
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{1.0, 0.0, 1.0, 1.0, 0.0,
Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
Double.MIN_VALUE};
frmTechyrajeev.getContentPane().setLayout(gridBagLayout);
JLabel lblLoginForm = new JLabel("Login Form");
GridBagConstraints gbc_lblLoginForm = new GridBagConstraints();
gbc_lblLoginForm.insets = new Insets(5, 0, 5, 5);
gbc_lblLoginForm.gridx = 2;
gbc_lblLoginForm.gridy = 0;
frmTechyrajeev.getContentPane().add(lblLoginForm, gbc_lblLoginForm);
JLabel lblEnterUsername = new JLabel("Username:");
GridBagConstraints gbc_lblEnterUsername = new GridBagConstraints();
gbc_lblEnterUsername.anchor = GridBagConstraints.EAST;
gbc_lblEnterUsername.insets = new Insets(0, 0, 5, 5);
gbc_lblEnterUsername.gridx = 1;
gbc_lblEnterUsername.gridy = 1;
frmTechyrajeev.getContentPane().add(lblEnterUsername, gbc_lblEnterUsername);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 5);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 2;
gbc_textField.gridy = 1;
frmTechyrajeev.getContentPane().add(textField, gbc_textField);
textField.setColumns(10);
JLabel label = new JLabel("");
GridBagConstraints gbc_label = new GridBagConstraints();
gbc_label.insets = new Insets(0, 0, 5, 5);
gbc_label.gridx = 1;
gbc_label.gridy = 2;
frmTechyrajeev.getContentPane().add(label, gbc_label);
JLabel lblPassword = new JLabel("Password:");
GridBagConstraints gbc_lblPassword = new GridBagConstraints();
gbc_lblPassword.anchor = GridBagConstraints.EAST;
gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
gbc_lblPassword.gridx = 1;
gbc_lblPassword.gridy = 3;
frmTechyrajeev.getContentPane().add(lblPassword, gbc_lblPassword);
textField_1 = new JTextField();
GridBagConstraints gbc_textField_1 = new GridBagConstraints();
gbc_textField_1.insets = new Insets(0, 0, 5, 5);
gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_1.gridx = 2;
gbc_textField_1.gridy = 3;
frmTechyrajeev.getContentPane().add(textField_1, gbc_textField_1);
textField_1.setColumns(10);
JButton btnLogin = new JButton("Login");
GridBagConstraints gbc_btnLogin = new GridBagConstraints();
gbc_btnLogin.insets = new Insets(0, 0, 5, 5);
gbc_btnLogin.gridx = 2;
gbc_btnLogin.gridy = 4;
frmTechyrajeev.getContentPane().add(btnLogin, gbc_btnLogin);
JTextArea txtrThisIsA = new JTextArea();
txtrThisIsA.setText("This is a text area.");
GridBagConstraints gbc_txtrThisIsA = new GridBagConstraints();
gbc_txtrThisIsA.gridwidth = 5;
gbc_txtrThisIsA.fill = GridBagConstraints.BOTH;
gbc_txtrThisIsA.gridx = 0;
gbc_txtrThisIsA.gridy = 6;
frmTechyrajeev.getContentPane().add(txtrThisIsA, gbc_txtrThisIsA);
}
}
This is the grid view of rows and columns of the frame.As you can see it is very easy to divide the whole Gui into rows and columns on the paper and then proceed accordingly for the actual designing using GridBagLayout Manager.
Output of this GridBagLayout tutorial program is: