Posts which you will also like.

Saturday, September 22, 2012

Java Tutorial on GridBagLayout Manager

In this java tutorial we are going to discuss about GridBagLayout Manager.
GridBagLayout manager is one of the most powerful but complex layout manager in java.It is somewhat similar to GridLayout in the sense that it also arranged the components in the grid, but here 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).
Most important parts of the GridBagLayout are GridBagConstraints .They specify the size and position, and many more constraints  of the each component. We will discuss about each GridBagConstraints fields.

gridx The column in which the component will be placed
gridy The row in which the component will be placed.
gridwidth The no of column the component occupies.
gridheight The no of rows the component occupies.
weightx The amount of extra space to allocate horizontally. The grid
slot can become wider when extra space is available.
weighty The amount of extra space to allocate vertically. The grid slot
can become taller when extra space is available.
anchor When component added is smaller than its area then we can use anchor to specify its position.Possible positions are NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST,
fill This basically tell how to resize the component when container area is bigger than it’s size.
We can have a single “GridBagConstraints” instance which is used for specifying the constraints of all the components or seperate “GridBagConstraints” for each of them.
But it is recommended to use seperate “GridBagConstraints” for each of the component added to the container using GridBagLayout Manager.
Now we will use the GridBagLayout in our sample program:

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();
                } catch (Exception e) {

     * Create the application.
    public GridBagLayoutTest() {

     * Initialize the contents of the frame.
    private void initialize() {
        frmTechyrajeev = new JFrame();
        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,
        gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
        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);
        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);
        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:


1 comment:

  1. NICE BLOG!!! Thanks for sharing useful information about FNT Softwre Solutions and being one of best Software Training institute in Bangalore we agree that this blog is very useful for the students who are searching for best software courses, I would really like to come back again right here for like wise good articles or blog posts. Thanks for sharing...Java training company bangalore.


Your comment may wait for moderation.... Protected by Copyscape Online Plagiarism Tool