Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

What Is Layout?

This section describes AWT layout classes that can be used to layout components in a container. Commonly used layouts are BorderLayout, FlowLayout, BoxLayout, GridLayout, and GridBagLayout.

What Is Layout? A layout is a set of rules that defines how graphical components should be positioned in a container.

There two ways to position a component is a container:

  • Using a predefined layout and allowing the layout to decide where to position the component. This is a soft way of positioning a component. If the container changes its size, the component's position will be adjusted. But you may not able to get precisely where you want to component to be.
  • Specifying the position of the component using the container's coordinates. This is a hard way of positioning a component. You can get precisely where you want the component to be. But if the container changes its size, the component's position will not be adjusted.

AWT offers a number of predefined layouts for you to use:

  • java.awt.BorderLayout - Divides the container into five regions: east, south, west, north, and center and assigns one component for each region.
  • java.awt.FlowLayout - Takes unlimited number of components and let them flow naturally horizontally first, then vertically.
  • java.awt.BoxLayout - Takes unlimited number of components and let them flow horizontally or vertically in one direction.
  • java.awt.GridLayout - Divides the container into rows and columns and assigns one component for each cell.
  • java.awt.GridBagLayout - Divides the container into rows and columns and assigns one component for each cell with cell sizes not equal.

Last update: 2009.

Sections in This Chapter

What Is Layout?

java.awt.BorderLayout - Border Layout

java.awt.FlowLayout - Flow Layout

java.awt.BoxLayout - Box Layout

java.awt.GridLayout - Grid Layout

java.awt.GridBagLayout - Grid Bag Layout

Dr. Herong Yang, updated in 2009
What Is Layout?