Java Layout Managers

prepare7 min read

Basics of Layout Managers:

Java technology uses Layout Managers to define the location and size of Graphical User Interface components. Java technology does not encourage programmers to use absolute location and size of components. Java technology instead places components based on specified Layout Manager. A Layout Manager implements a layout policy that defines constraints between components in a container.

Types of Layout Managers:

Java technology provides the following Layout Managers, each of which implements the LayoutManager interface.

FlowLayout

GridLayout

BorderLayout

GridBagLayout

CardLayout

The FlowLayout is the default Layout Manager for Panel, and hence the Applet class. The BorderLayout is the default Layout Manager for Window class and its subclasses (Frame and Dialog).

Setting Layout Managers:

The following method defined in the Container class can be used for setting layout managers.

void setLayout(LayoutManager mgr);

So for example to set FlowLayout as the Layout Manager for a container C, the following can be used –

C.setLayout(new FlowLayout());

The add method defined in the Container class can be used for adding components to a container. The following are the prototypes of the add method –

The order in which components are added to a container effects the placement of components. Also a component added to a container can itself be a container that holds other components.

FlowLayout Manager:

FlowLayout places component in rows from left to right. Components towards the end of row are written on next row, if there is not enough space in the current row. The FlowLayout honors the specified size of a component. The size of a component never changes regardless of size of container. The following constructors of FlowLayout are provided by AWT –

Alignment can take values of constants – LEFT, CENTER and RIGHT. The default alignment for the components in a row is center. Default horizontal and vertical gaps are 5 pixels.

GridLayout Manager:

A GridLayout Manager places the components in a rectangular grid. Each component’s position is identified by a column and row. All the cells in the grid have same size and width. Each component is stretched to the cell size. So a GridLayout ignores the Component’s preferred size.The GridLayout class provides the following constructors.

This creates a row*col grid layout with the specified horizontal and vertical gaps. In the constructor, either rows or cols can be zero, but not both. The first constructor is equivalent to one row with any number of components. The default gap between components is zero pixels.

BorderLayout Manager:

A BorderLayout Manager divides the window into five regions – North, East, West, South and Center. A component can be explicitly added to one of the regions using the add() method of the Container class. Any space left over by the component in North, East, South and West is occupied by the component in Center. Only the last component added to a region is shown, and not all regions need to be occupied by a component. So a container that uses BorderLayout may have up-to 5 components.The BorderLayout class defines the following constructors –

BorderLayout();
BorderLayout(int hor_gap, int ver_gap);

As illustrated below, components can be added to the container using add() method defined in the Container class.

Here constraints can be NORTH, SOUTH, EAST, WEST and CENTER. These constants correspond to strings “North”, “South”, East”, “West”, and “Center” respectively. They describe the region where the component should be placed. The default region is CENTER.

GridBagLayout:

GridBagLayout is the most advanced LayoutManager in Java technology. Refer to The Java AWT: GridBagLayout for an excellent coverage of GridBagLayout. Typically the Certification exam includes one question on GridBagLayout.