AWT Layout Managers: FlowLayout, GridLayout, BorderLayout & CardLayout
1. What is a Layout Manager in AWT?
When you add buttons, labels, and text fields to a Frame or Panel, instead of manually calculating pixel coordinates (x, y, width, height) for every component (which breaks when a user resizes the window on different screen resolutions), Java uses a Layout Manager.
A Layout Manager is an object (implementing the LayoutManager interface) that automatically controls the size, position, and arrangement of components inside a Container.
- We set a container’s layout manager using the
setLayout(LayoutManager mgr)method:setLayout(new FlowLayout());
Figure 3.6: The 4 Standard AWT Layout Managers in Your Syllabus
1. FlowLayout
Arranges components left-to-right in rows like words in a paragraph (Default for Panel/Applet)
2. BorderLayout
Divides container into 5 zones: NORTH, SOUTH, EAST, WEST, CENTER (Default for Frame)
3. GridLayout
Divides container into a rectangular matrix of equal-sized cells (Rows × Columns)
4. CardLayout
Stacks components one behind another like a deck of playing cards (Only 1 visible at a time)
2. FlowLayout (Left-to-Right Row Arrangement)
- How it works:
FlowLayoutis the simplest layout manager. It arranges components in a directional flow from left to right, just like words in a text editor. When the current horizontal row fills up, it automatically wraps the next component onto a new line! - Default Layout For:
PanelandApplet. - Alignment Constants:
FlowLayout.LEFT,FlowLayout.CENTER(default),FlowLayout.RIGHT.
Constructors of FlowLayout:
FlowLayout()— Centers components with a default 5-pixel horizontal and vertical gap.FlowLayout(int align)— Specifies alignment (LEFT,CENTER, orRIGHT).FlowLayout(int align, int hgap, int vgap)— Specifies alignment and custom pixel gaps.
Code Example:
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
add(new Button("Button 1"));
add(new Button("Button 2"));
add(new Button("Button 3"));
3. BorderLayout (5 Geographic Regions)
- How it works:
BorderLayoutdivides the container into 5 distinct regions:NORTH(Top),SOUTH(Bottom),EAST(Right),WEST(Left), andCENTER(Middle). - Default Layout For:
Frame,Window, andDialog. - Important Rule: Each region can hold only ONE component (to put multiple buttons in the
SOUTHregion, put them inside aPanelfirst, and add thatPaneltoBorderLayout.SOUTH). If you don’t specify a region inadd(), the component goes intoCENTERby default and stretches to fill all remaining space!
Figure 3.7: The 5 Regions of BorderLayout
TopBorderLayout.NORTH
Spans full width at the top (Ideal for Toolbars / Headers)
MidWEST | CENTER | EAST
WEST (Left Sidebar) | CENTER (Main Content Area) | EAST (Right Sidebar)
BotBorderLayout.SOUTH
Spans full width at the bottom (Ideal for Status Bar / Action Buttons)
Complete Code Example (BorderLayout):
import java.awt.*;
public class BorderLayoutDemo extends Frame {
public BorderLayoutDemo() {
setTitle("BorderLayout Example");
setSize(400, 300);
setLayout(new BorderLayout(5, 5)); // 5px horizontal & vertical gap
add(new Button("NORTH (Header)"), BorderLayout.NORTH);
add(new Button("SOUTH (Status Bar)"), BorderLayout.SOUTH);
add(new Button("EAST"), BorderLayout.EAST);
add(new Button("WEST"), BorderLayout.WEST);
add(new Button("CENTER (Main Workspace)"), BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutDemo();
}
}
4. GridLayout (Equal-Sized Rows × Columns Matrix)
- How it works:
GridLayoutdivides the container into a 2D grid (table) of rows and columns. - Key Characteristic: Every cell in a
GridLayoutis forced to be of the exact same width and height, and each component stretches to completely fill its cell! (Ideal for building a Calculator keypad or Tic-Tac-Toe board).
Constructors of GridLayout:
GridLayout(int rows, int cols)— Creates a grid with specified rows and columns.GridLayout(int rows, int cols, int hgap, int vgap)— Adds horizontal and vertical spacing between cells.
Code Example (3×3 Calculator Keypad):
setLayout(new GridLayout(3, 3, 5, 5)); // 3 rows, 3 columns, 5px gap
for (int i = 1; i <= 9; i++) {
add(new Button("" + i));
}
5. CardLayout (Stack of Cards — Wizard / Tab View)
- How it works:
CardLayouttreats each component (usually aPanel) as a card in a deck of playing cards stacked one on top of another. - Key Characteristic: Only ONE card (panel) is visible on screen at any given time! You can flip between cards using built-in navigation methods—just like a Next / Previous Installation Wizard or tabbed screens.
Important Navigation Methods of CardLayout:
first(Container parent): Flips to the very first card in the deck.next(Container parent): Flips to the next card.previous(Container parent): Flips to the previous card.last(Container parent): Flips to the last card.show(Container parent, String cardName): Jumps directly to a specific card by its name!
Code Snippet Demonstrating CardLayout:
CardLayout card = new CardLayout();
Panel deckPanel = new Panel(card);
deckPanel.add("Step1", new Button("Card 1: Personal Details (Click for Next)"));
deckPanel.add("Step2", new Button("Card 2: Academic Details"));
deckPanel.add("Step3", new Button("Card 3: Payment Confirmation"));
// Flip to the next card in the deckPanel
card.next(deckPanel);
6. Quick Summary Table of All 4 Layout Managers
| Layout Manager | Arrangement Style | Component Sizing | Best Real-Life Use Case |
|---|---|---|---|
FlowLayout |
Left-to-right row by row; wraps when full. | Keeps each component’s natural preferred size. | Simple forms or a row of buttons inside a Panel. |
BorderLayout |
5 regions: NORTH, SOUTH, EAST, WEST, CENTER. |
CENTER stretches both ways; NORTH/SOUTH stretch horizontally. |
Main application window (Frame) layout. |
GridLayout |
Rectangular matrix of rows × cols. |
Forces all components to be the exact same size. | Calculator keypads, Chess/Tic-Tac-Toe grids. |
CardLayout |
Stacked one behind another; only 1 visible at a time. | Each card fills the entire container area. | Step-by-step “Next / Back” installation wizards. |
💡 Exam Preparation Tips for DAVV BCA
- Remember the two default layouts for 2-mark questions:
- Default layout of
FrameisBorderLayout. - Default layout of
PanelandAppletisFlowLayout.
- Default layout of
- When explaining
BorderLayoutandGridLayoutin an 8-mark question, always draw a simple box showing the 5 zones (NORTH,WEST,CENTER,EAST,SOUTH) and a 3× 3 grid!