You are on page 1of 19

TCS Confidential

1
Layout Managers
Java Programming
TCS Confidential
2
Arranging GUI Components
Need of arranging components
Use layout manager classes
LayoutManager interface
designed with AWT
Layout Managers: FlowLayout,
GridLayout, BorderLayout,
CardLayout,
GridBagLayout,BoxLayout
TCS Confidential
3
Flow Layout Manager
Components go from left to right in
the order they are added to container
and immediately below when there is
no room
Constructors are:
FlowLayout(), FlowLayout(int align)
align can be CENTER or RIGHT or
LEFT to align the components along
the center or the left or right edge of
container
TCS Confidential
4
Demo: RowCenter.java
Adds three controls: Label, TextField
and a Button
Resizing window shows how FlowLayout
re-arranges controls
FlowLayout Manager Usage
TCS Confidential
5
RowCenter.java ...
TCS Confidential
6
Grid Layout Manager
GridLayout manager
arranges components into a
grid of rows and columns
They are added first to the
top row from left to right,
then to the next row, and so
on
TCS Confidential
7
Using Grid Layout Manager
The constructors:
GridLayout(int nRow, int nCol)
GridLayout(int nRow, int nCol, int
hg, int vg)
nRow, nCol are the number of
rows and columns; hg, vg specify
the horizontal and vertical gap in
pixels between components
TCS Confidential
8
Demo GridLayout
Demo of Grid4x3Demo.java
Uses 4 rows and 3 column GridLayout
Adds A Label, TextField and a Button in
each row
TCS Confidential
9
Demo of Grid4x3Demo.java ...
TCS Confidential
10
The Border Layout Manager
implements a common layout
style for top-level windows
It has 4 narrow, fixed-width
components at the edges
referred as north, south, east,
and west, and one large area
in the center
Border Layout Manager
TCS Confidential
11
Demo BorderLayout
Sets BorderLayout
init() method adds 3 Buttons
in West, North and East, a
Label is added in South and a
TextArea is added in Center
TCS Confidential
12
Demo: BorderLayoutDemo.java
TCS Confidential
13
Advanced Layout Managers
The CardLayout manager is
unique in that it stores several
different layouts
Each layout can be thought of
as being on a separate index
card in a deck that can be
shuffled so that any card is on
top at a given time
TCS Confidential
14
Advanced Layout Managers
This can be useful for user
interfaces with optional
components that can be
dynamically enabled/disabled on
user input
Card Layout
Panel
Card-1
Card-2
Card-3
TCS Confidential
15
Advanced Layout Managers
The GridBagLayout manager is an
extension of the GridLayout manager:
A component can take up more than
one columns
The proportions between different
rows and columns do not have to be
equal
Components inside grid can be
arranged in different ways
Greater Control on layout then other
layout managers
TCS Confidential
16
How to Choose Layout Manager
You need to display a component in as
much space as it can get.
You need to display a few components
in a compact row at their natural size.
You need to display a few components
of the same size in rows and columns.
TCS Confidential
17
How to Choose Layout Manager
You need to display a few components
in a row or column, possibly with
varying amounts of space between them,
custom alignment, or custom
component sizes
You have a complex layout with many
components.
TCS Confidential
18
The BoxLayout class puts components in
a single row or column. It respects the
components' requested maximum sizes,
and also lets you align components.
BoxLayout Manager
TCS Confidential
19
Question & Answers

You might also like