Layout Managers

A layout manager implements a layout policy that defines spatial relationships between components in a container. Once a layout manager is registered with a container and components have been added, the layout manager is responsible for the placement and sizing of the components. Applications never call the methods of the layout manager directly. The container calls the appropriate methods in the layout manager when necessary. The Java AWT package provides five layout managers.

FlowLayout: Lays out the components in row-major order. Default layout manager for Applet classes. The FlowLayout class provides constructors that allow the alignment, horizontal, and vertical gaps for the layout to be specified

FlowLayout()
FlowLayout (int alignment)
FlowLayout (int alignment, int horizontalgap, int verticalgap)

GridLayout: Lays out the components in a specified rectangular grid. The GridLayout class provides the following constructors:

GridLayout()
GridLayout (int rows, int columns)
GridLayout (int rows, int columns, int horizontalgap, int verticalgap)

BorderLayout: Up to five components can be placed in a container in the following directions - north, south, east, west, and center. The default region is the center. The BorderLayout class has the following constructors:

BorderLayout()
BorderLayout (int horizontalgap, int verticalgap)

void add (Component comp, Object constraints)

CardLayout: Components are handled as a stack of indexed cards with only the top component being visible in the container. The CardLayout does not give any visual clue that the container consists of a stack of components. The CardLayout class has the following constructors:

CardLayout()
CardLayout (int horizontalgap, int verticalgap);

GridBagLayout: Customizable and flexible layout manager that lays out components in a rectangular grid. A component can occupy multiple cells in the grid but the region it occupies is always rectangular. To set up a GridBagLayout the following steps have to be taken:

  1. Create an object of the class GridBagLayout
    GridBagLayout gbl = new GridBagLayout();
  2. Set the layout manager for the container
    container.setLayout(gbl);
  3. Create an object of the class GridBagConstraints
    GridBagConstraints gbc = new GridBagConstraints();
  4. For each component to be added fill in the layout information in the GridBagConstraints object and add the component.
    Container.add (new Checkbox("Fill", false), gbc);

The key to using the grid bag layout is to understand how the GridBagConstraints object affects the layout. A GridBagConstraints object can be constructed using the following constructors:

GridBagConstraints()
GridBagConstraints (int gridx, int gridy, int gridwidth, int gridheight, 
		    double weightx, double weighty, int anchor, int fill, 
		    Insets insets, int ipadx, int ipady)

Common Methods for designing a Layout

Getting and setting a Layout Manager
	LayoutManager  getLayout()
	void setLayout (LayoutManager mgr)

Adding components

	Component add (Component comp)
	void add (Component comp, Object constraints)

Removing components

	void remove (Component comp)
	void removeAll ()

For simple applications, the components can be put directly into an applet or a single frame. However, components can be placed into containers, which in turn can be put into other containers, resulting in a component hierarchy. This should not be confused with inheritance hierarchy.

The following program illustrates the use of Layout Managers. To run the program uncomment the section on a particular Layout Manager and comment out those that you do not want. Compile the program and run it.

import java.awt.*;

public class LayoutManagerDemo 
{
  public static void main ( String [] args ) 
  {
    //  Create the frame
    Frame frame = new Frame ( "Layout Manager Demo" );
    
    /*  Demonstrate Flow Layout  
    CheckboxGroup size = new CheckboxGroup ();
	
    Checkbox cb1 = new Checkbox ("Large", size, false);
    Checkbox cb2 = new Checkbox ("Medium", size, false);
    Checkbox cb3 = new Checkbox ("Small", size, false);
		
    frame.setLayout ( new FlowLayout () );
    frame.add (cb1); frame.add (cb2); frame.add (cb3);		
    frame.pack ();
    frame.setVisible ( true ); 
    */
		
    /*  Demonstrate Grid Layout  
    Label xLabel = new Label ("X Coordinate");
    Label yLabel = new Label ("Y Coordinate");
    TextField xInput = new TextField (5);
    TextField yInput = new TextField (5);
		
    frame.setLayout (new GridLayout(2,2,20,20));
    frame.add(xLabel);  frame.add(xInput);
    frame.add(yLabel);  frame.add(yInput);
    frame.pack ();
    frame.setVisible ( true );
    */
		
    /*  Demonstrate Border Layout 
    TextField header = new TextField ("Title");
    header.setEditable (false);
		
    TextField footer = new TextField ("Footnote");
    footer.setEditable (false);
		
    Scrollbar sb1 = new Scrollbar (Scrollbar.VERTICAL, 0, 1, 0, 255);
    Scrollbar sb2 = new Scrollbar (Scrollbar.VERTICAL, 0, 1, 0, 255);
		
    Canvas drawRegion = new Canvas();
    drawRegion.setSize (150,150);
    drawRegion.setBackground (Color.red);
		
    frame.setLayout (new BorderLayout());
    frame.add (header, BorderLayout.NORTH);
    frame.add (footer, BorderLayout.SOUTH);
    frame.add (sb1, BorderLayout.WEST);
    frame.add (sb2, BorderLayout.EAST);
    frame.add (drawRegion, BorderLayout.CENTER);
    frame.pack ();
    frame.setVisible ( true );
    */
		
    /*  Demonstrate Grid Bag Layout  */ 
    String colors[] = {"Violet", "Indigo", "Blue", "Green", 
		       "Yellow", "Orange", "Red"};
    List colorList = new List (colors.length-2, false);
    for (int i = 0; i < colors.length; i++)
	colorList.add (colors[i]);
			
    Label shape = new Label ("Shape");
    shape.setFont (new Font("Times", Font.BOLD, 14));
		
    Choice shapeChoice = new Choice();
    shapeChoice.add("Triangle");  shapeChoice.add("Square");
    shapeChoice.add("Rectangle");  shapeChoice.add("Polygon");
		
    Checkbox cbFill = new Checkbox ("Fill", false);
    cbFill.setFont (new Font("Times", Font.BOLD, 14));
		
    frame.setLayout (new GridBagLayout());
		
    GridBagConstraints gbc = new GridBagConstraints();
		
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1;  gbc.weighty = 0;

    addUsingGBL (frame, colorList, gbc, 0, 0, 1, 3);
    addUsingGBL (frame, shape, gbc, 1, 0, 1, 2);
    addUsingGBL (frame, shapeChoice, gbc, 2, 0, 1, 2);
    addUsingGBL (frame, cbFill, gbc, 1, 2, 2, 1);

    frame.pack ();
    frame.setVisible ( true );
    }
	
    public static void addUsingGBL (Frame frame, Component component, 
				    GridBagConstraints gbc, int x, 
				    int y, int w, int h) 
    {
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = w;
      gbc.gridheight = h;
      frame.add (component, gbc);
    }

}