Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

April 12, 2016 Categories: AWT. No Comments on Layout Managers

In this article we will learn about what is a layout manager and what are the different layout managers available in Java.

 

A layout manager is one which automatically manages the arrangement of various of components in a container. Each container will have a default layout manager. Default layout managers for some of the container classes are given below:

Panel – Flow Layout

JPanel – Flow Layout

Applet – Flow Layout

JApplet – Border Layout

Frame – Border Layout

JFrame – Border Layout

A layout manager is an instance of that class which implements the LayoutManager interface. The layout manager can be set by using the setLayout() method whose general form is as follows:

void setLayout(LayoutManager layoutObj)

We can manually arrange the position of each component (not recommended) by passing null to setLayout() method and by using setBounds() method on each component. Different layout managers available in AWT are:

  1. FlowLayout
  2. BorderLayout
  3. GridLayout
  4. CardLayout
  5. GridBagLayout

 

FlowLayout Manager

The flow layout manager arranges the components one after another from left-to-right and top-to-bottom manner. The flow layout manager gives some space between components. Flow layout manager instance can be created using anyone of the following constructors:

FlowLayout()

FlowLayout(int how)

FlowLayout(int how, int hspace, int vspace)

In the above constructors, how specifies the alignment, hspace  specifies horizontal space, and vspace specifies vertical space. Valid values for alignment are as follows:

FlowLayout.LEFT

FlowLayout.CENTER

FlowLayout.RIGHT

FlowLayout.LEADING

FlowLayout.TRAILING

Following code demonstrates working with FlowLayout:

Output of the above code is as shown below:

flowlayout


 

BorderLayout Manager

The border layout manager divides the container area into five regions namely: north, south, east, west, and center. Default region is center. You have to be careful with border layout as controls might be stacked over one another. Border layout instance can be created by using one of the below constructors:

BorderLayout()

BorderLayout(int hspace, int vspace)

In the above constructors, hspace signifies horizontal space between components and vspace signifies vertical space between components.

Following code demonstrates working with BorderLayout:

Output of the above code is as shown below:

borderlayout


 

GridLayout Manager

The grid layout manager arranges the components in a 2-dimensional grid. While creating the instance of GridLayout, we can specify the number of rows and columns in the grid. Care must be taken with the number of cells in the grid and the number of components being added to the grid. If they don’t match, we might get unexpected output.

Am instance of GridLayout can be created using one of the following constructors:

GridLayout()

GridLayout(int numRows, int numCols)

GridLayout(int numRows, int numCols, int hspace, int vspace)

In the above constructors, numRows and numCols specifies the number of rows and columns in the grid, hspace and vsapce specifies the horizontal space and vertical space between the components.

Following code demonstrates working with GridLayout:

Output of the above code is as shown below:

gridlayout


 

CardLayout Manager

The card layout manager allows the user to create a deck of cards. Each card can contain different components. At any instant only one card in the deck can be displayed.

To implement card layout, we must take a panel which acts as the container for other cards. Each card in turn can be a panel which can contain different components. Components will be added to the respective cards (panels) and all the cards will be finally added to the deck (container panel).

The card layout can be instantiated using any one of the following constructors:

CardLayout()

CardLayout(int hspace, int vspace)

When adding the cards (panels) to the deck, the following add() method can be used:

void add(Component panelRef, Object name)

In the above syntax, name is a string which represents the name of the card (panel). After adding all the cards (panels) to the deck, we can navigate through the cards using the following methods available in CardLayout class:

void first(Container deck)

void last(Container deck)

void next(Container deck)

void previous(Container deck)

void show(Container deck, String cardName)

Following code demonstrates working with CardLayout:

Output of the above code is as shown below:

cardlayout


 

GrdiBagLayout Manager

The grid bag layout manager can be used to create an uneven grid i.e., number of columns in each row can differ. Also the size of components within a cell can be different.

The location and size of each component are specified by a set of constraints that are contained in an object of type GridBagConstraints. These constraints include height, width, and placement of a component.

The general process to work with GridBagLayout is, first set the layout of the container to GridBagLayout, then set the constraints for each component using GridBagConstraints, and then add each component to the container.

GridBagConstraints class specifies the following fields which can be used to set constraints on each component:

gridbag-constraints

Following code can be used to work with GridBagLayout:

Output of the above code is as shown below:

gridbag-layout

 

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up