Grid Bag Layout in Java With Examples

Grid Bag Layout With Examples:


logo

Table of Contents

a) Introduction of GridBag Layout
b) Different types of Constructors
c) Source Code for each Constructor
d) Output of each Constructor

********************
Introduction:

Grid Bag Layout is the upgraded form of Grid Layout. Unlike Grid layout, The size of the components can be changed here and can occupy the multiple rows or columns. Unlike any other layout managers, you can add the components in any order you like because in other layout managers the components should be added serially to get the desired output. The GridBagLayout class is used to align the components vertically, horizontally or along their baseline. The each components of the GridBagLayout maintains a dynamic , rectangular grid of cells. The GridBag Layout needs the helper class called GridBagConstraints. Without the GridBagConstraints we cannot implement the GridBag Layout. The GridBagConstraints have the following methods inside, they are: gridx, gridy, gridwidth, gridheight and fill.

    gridx : determines the x-axis.
    gridy: determines the y-axis.
    gridwidth: determines the width occupied by the components.
    gridheight: determines the height occupied by the components.
    fill: determines the horizontal , vertical or both alignment.

[ Note: if we use gridwidth then fill= HORIZONTAL, gridheight then fill=VERTICAL and if  gridwidth and gridheight both is used then fill=BOTH. ]

The GridBagLayout has only one constructor i.e;

     GridBagLayout()

Let’s see the source code and the output of the GridBagLayout;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package LayoutManagers;

/**
 *
 * @author AnkitPC
 */
import javax.swing.*;
import java.awt.*;
public class GridBagLayoutDemo {
    JFrame f1;
    JButton b1,b2,b3,b4,b5,b6,b7;
     GridBagLayoutDemo(){
         f1=new JFrame("GridBagLayout");
         f1.setSize(400,300);
         f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
         GridBagLayout g=new GridBagLayout();
         f1.setLayout(g);
        
         GridBagConstraints ob= new GridBagConstraints();
        
         b1=new JButton("1");
         b2=new JButton("2");
         b3=new JButton("3");
         b4=new JButton("4");
         b5=new JButton("5");
         b6=new JButton("6");
         b7=new JButton("7");
        
         ob.gridx=0;
         ob.gridy=0;
         g.setConstraints(b1,ob);
         f1.add(b1);
        
         ob.gridx=1;
         ob.gridy=0;
         g.setConstraints(b2,ob);
         f1.add(b2);
        
         ob.gridx=2;
         ob.gridy=0;
         g.setConstraints(b3,ob);
         f1.add(b3);
        
         ob.gridx=2;
         ob.gridy=1;
         g.setConstraints(b5,ob);
         f1.add(b5);
        
         ob.gridx=2;
         ob.gridy=2;
         g.setConstraints(b6,ob);
         f1.add(b6);
        
         ob.gridx=0;
         ob.gridy=3;
         ob.gridwidth=3;
         ob.fill=GridBagConstraints.HORIZONTAL;
         g.setConstraints(b7,ob);
         f1.add(b7);
        
        
         ob.gridx=0;
         ob.gridy=1;
         ob.gridwidth=2;
         ob.gridheight=2;
         ob.fill=GridBagConstraints.BOTH;
         g.setConstraints(b4,ob);
         f1.add(b4);
        
 
        
        
         f1.setVisible(true);
     }
   
   
   
   
    public static void main(String[] args) {
        new GridBagLayoutDemo();
    }

   
}       

Let's see how these  gridxgridygridwidthgridheight and fill works;

gridbaglayout

The above source code demonstrate the use of Grid Bag Layout. Before the output, you can see that gridx works from left to right and gridy works from top to bottom. gridwidth specifies number of horizontal space occupied by the component and gridheight specifies the number of vertical space occupied by the component. As you can see in the source code that, i have added the button 4 at last. It is because that we can add the component as we like. Unlike any other layout managers it gives freedom to arrange the components. So it is the most flexible but difficult to use.
                     
Output:


gridbaglayout



As you can see there is massive difference between the GridLayout() and GridBagLayout(). Here are the some differences between the GridLayout() and GridBagLayout();


Grid Layout

Grid Bag Layout
GridLayout divides the window in equal-sized rectangles upon the number of rows and columns i.e arranges the component in the form of matrix.

GridBagLayout divides the window into grids, and does not force the component to be of same size.
No any helper class is used.
GridBagConstraints helper class is used here.
The size of the components cannot be altered.

The size of the components can be altered.
It is not flexible as compared to GridBagLayout.
It is more flexible than any other layout managers.

***********

Post a Comment

0 Comments