Java AWT GridBagLayout Manager


Using Two Properties

To start with, let us make a simple program with two buttons. To understand the constraints, only two properties girdx, gridy and fill are used.

import java.awt.*;   
public class TwoConstraints extends Frame  
{
  public TwoConstraints()  
  {
    GridBagLayout gbl = new GridBagLayout();    // create an object of grid bag layout
    GridBagConstraints gbc = new GridBagConstraints(); //grid bag constraints object 
    setLayout(gbl);                          

    Button b1 = new Button("Button 1");             // start giving constraints to button 1
    gbc.weightx = 1.0;              // maximum values are set to weight variables initially
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.BOTH;                   // component grows both ways
    gbl.setConstraints(b1, gbc);       // informing the grid bag layout to set the button b1 
    add(b1);                          // as per gbc constraints

    Button b2 = new Button("Button 2");
    gbc.weightx = 1.0;               // maximum values are set to weight variables initially
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.BOTH; // this component also expands both ways
    gbl.setConstraints(b2, gbc);  // informing the grid bag layout about the button b2
    add(b2);                                 // and its constraints

    setTitle("TwoConstraints");  
    setSize(300, 300);   
    setVisible(true);
  }
  public static void main(String args[])   
  {   
    new TwoConstraints();  
  }
}

Output screen of TwoConstraints.java

Both the buttons in the above program are of the same size and initially set with same constraints. gridx and gridy constraints are not set and will take the default values. For the first button it is 0 and 0 and for the second button it is relative to the first button (means added implicitly next to the previous).

For both the buttons weightx and weighty are given same values. To understand the effect of these variables, keep one button values as it is and change the other button values of both weightx and weighty (at the beginning change one only). With all the possible combinations change and see the output. Remember the values must be within 0.0 and 1.0.

To try with other variable, come back to the original program (with original values). Next attack the other variable fill. Keep one button’s constraints as it is and change the other one. The other possible values for fill are as follows:

GridBagConstraints.HORIZONTAL: Component grows horizontally
GridBagConstraints.VERTICAL: Component grows vertically
GridBagConstraints.BOTH: Component grows both ways
GridBagConstraints.NONE: Component does not grow at all

When satisfied with the observation, change both weightx, weighty and fill and observe the effect. This is very essential to understand the next program.

Using all the Properties

The grid bag layout is based on the idea of rows and columns. When you are placing the components in grid layout or flow layout, the order of addition is very important. But is not the case in GridBagLayout because we specify the cell position of the component in terms of gridx (column number) and gridy (row number). That is, we can add the last button and then the first button.

In the following program, 7 buttons of different sizes are created and added.

import java.awt.*;    
import java.awt.event.*;
public class IrregularSizeButtons extends Frame  
{
  public IrregularSizeButtons()   
  {
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    setLayout(gbl);
    Button b ;    // a reference variable of  Button which can be instantiated many times if reinstantiated, the earlier object is garbage collected
    gbc.gridx = 0;   	             // start setting the constraints                                                           
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;   // grows horizontally

    b = new Button("First");
    gbl.setConstraints(b, gbc);
    add(b);

    b = new Button("Second");
    gbc.gridx = 1;            // gbc.gridy = 0 is taken from the previous button.  If ones the the constraint is set, it will remain same in the        
    gbl.setConstraints(b, gbc);         // subsequent code, unless it is changed
    add(b);

    b = new Button("Third");
    gbc.gridx = 2;
    gbc.gridwidth = GridBagConstraints.REMAINDER;    // REMAINDER indicates to 
    gbl.setConstraints(b, gbc);      // the layout that this component is last one in the row
    add(b);

    b = new Button("Fourth");
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbl.setConstraints(b, gbc);                           
    add(b);   

    b = new Button("Fifth");
    gbc.gridwidth = 1;
    gbc.gridy = 2;
    gbl.setConstraints(b, gbc);
    add(b);

    b = new Button("Six");
    gbc.gridx = 1;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbl.setConstraints(b, gbc);
    add(b);

    b = new Button("Seven");
    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.gridwidth = GridBagConstraints.REMAINDER ;
    gbl.setConstraints(b, gbc);
    add(b);
  }
  public static void main)String args[])   
  {
    Frame f = new IrregularSizeButtons();   
    f.setTitle("Creating Uneven size buttons");  
    f.setSize(300,300);  
    f.setVisible(true);
  }
}
Output screen of IrregularSizeButtons.java

Observe the button sizes. The first three and fifth take only one cell. The fourth and seventh occupies 3 cells and sixth one takes two cells. This type of irregular size components’ creation is possible with GidBagLayout only.

The variables gridx and gridy give the location where the component is to be positioned. Other way, they give the cell number. gridwidth and gridheight give the number of cells the component should occupy horizontally and vertically. These variables give different sizes to the components (in terms of cells). Another style of giving the width of the component is as in the following format.

gbc.gridwidth = GridBagConstraints.REMAINDER;

REMAINDER indicates that the component should occupy all the remaining cells available in the row. This is a better way of giving the size (or adding) if the component is the last one added in the row. By this, the GridBagLayout comes to an understanding that the row is over.

gbc.fill = GridBagConstraints.HORIZONTAL;

The above statement tells the GridBagLayout manager that the component can fill all the remaining cells in the row. It permits the component to occupy the remaining space of the row. The fill value HORIZONTAL must be used in combination with gridwidth value as REMAINDER; else no meaning.

Leave a Comment

Your email address will not be published.