GridBagLayout

The GridBagLayout manager is the most flexible but most complex of the layout mangers. This discussion is intended to get you started, but because so many constraints are provided and because they interact with one another, you should plan to spend time experimenting with different values and combinations.

The idea behind GridBagLayout is that you divide the display in cells (rows and columns), similar to a GridLayout. However, the difference is that the various cells can have different sizes. Furthermore, as the window is resized, the components within the cells are resized in accord with their respective constraints and not uniformly.

Like the CardLayout manager, you will need to create an instance variable of that type.

Most of the work in using GridBagLayout is in setting values for the different variables included in the GridBagConstraint class. Review the specification for that class.

Some of the variables that can be set in GridBagConstraints are the following:

Once the constraints are set for a given component, the setConstraints method is used for the GridBagLayout object to which the component and the constraints object are passed.

Below is code for a basic GridBag Layout. Note the addComponent class that is used to simplify the setting of constraints.

import java.applet.Applet;
import java.awt.*;

public class step5 extends Applet{

// Illustrates GridBagLayout 

Frame outerBox = new Frame ( );

Panel pad = new Panel ( );

Button button0 = new Button ( );

Button button1 = new Button ( "Button 1" );
Button button2 = new Button ( "Button 2" );
Button button3 = new Button ( "Button 3" );
Button button4 = new Button ( "Button 4" );
Button button5 = new Button ( "Button 5" );
Button button6 = new Button ( "Button 6" );

GridBagLayout gbLayout = new GridBagLayout ( );
GridBagConstraints gbConstraints = new GridBagConstraints ( );

  public void init ( ){

    button0.setBackground ( new Color ( 239, 192, 255 ) );

    button1.setBackground ( Color.red );
    button2.setBackground ( Color.lightGray );
    button3.setBackground ( Color.cyan );
    button4.setBackground ( Color.yellow );
    button5.setBackground ( Color.green );
    button6.setBackground ( Color.orange );

    pad.setBackground ( Color.blue);

    outerBox.setLayout ( gbLayout  );
    outerBox.setBackground ( Color.white );
    outerBox. setFont (new Font ("Helvetica", Font.PLAIN, 18) );
    outerBox.resize (600, 500);

    gbConstraints.insets= new Insets ( 0, 0, 20, 20);

    gbConstraints.anchor= GridBagConstraints.NORTHWEST;
    gbConstraints.fill = GridBagConstraints.BOTH;
    gbConstraints.weightx = 1000;
    gbConstraints.weighty = 1;
    addComponent ( outerBox, button0, gbLayout, gbConstraints, 0, 0, 3, 3 );

    gbConstraints.anchor= GridBagConstraints.NORTHEAST;
    gbConstraints.fill = GridBagConstraints.NONE;
    gbConstraints.weightx = 0;
    gbConstraints.weighty = 0;
    addComponent ( outerBox, button1, gbLayout, gbConstraints, 3, 0, 1, 1 );
    addComponent ( outerBox, button3, gbLayout, gbConstraints, 3, 1, 1, 1 );
    addComponent ( outerBox, button5, gbLayout, gbConstraints, 3, 2, 1, 1 );

    gbConstraints.anchor= GridBagConstraints.NORTHWEST;
    gbConstraints.weightx = 1000;
    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
    addComponent ( outerBox, button2, gbLayout, gbConstraints, 4, 0, 4, 1 );
    addComponent ( outerBox, button4, gbLayout, gbConstraints, 4, 1, 4, 1 );
    addComponent ( outerBox, button6, gbLayout, gbConstraints, 4, 2, 4, 1 );

    gbConstraints.anchor= GridBagConstraints.NORTHWEST;
    gbConstraints.fill = GridBagConstraints.BOTH;
    gbConstraints.weightx = 1000;
    gbConstraints.weighty = 1000;
    addComponent ( outerBox, pad, gbLayout, gbConstraints, 0, 3, 8, 1 );

    outerBox.show( );

  } // end init

  public void addComponent ( Container  cont, Component comp, GridBagLayout gbl, GridBagConstraints gbc, int col, int row, int wid, int hgt ) {

    gbc.gridx = col;
    gbc.gridy = row;
    gbc.gridwidth = wid;
    gbc.gridheight = hgt;
    gbl.setConstraints ( comp, gbc );
    cont.add ( comp );

  }  // end addComponent

}// end step5 

Run applet