Chapter 11 Layout Managers
· Precise layout functionality
is often performed and a repetitive task. By the principles of OOP, it should be
done by classes dedicated to it. These classes are layout managers.
· Platform independence
requires that we delegate the positioning and painting to layout managers. Even
then, Java does not guarantee a button will look the same in different
platforms.(w/o using Swing)
· Components are added to a
container using add method. A layout manager is associated with the container to
handle the positioning and appearance of the components.
· add method is overloaded.
Constraints are used differently by different layout managers. Index can be used
to add the component at a particular place. Default is –1 ( i.e. at the end)
Component
add(Component comp)
Component
add(Component comp, int index)
void
add(Component comp, Object constraints)
void
add(Component comp, Object constraints, int index)
· setLayout is used to
associate a layout manager to a container. Panel class has a constructor that
takes a layout manager. getLayout returns the associated layout manager.
· It is recommended that the
layout manager be associated with the container before any component is added.
If we associate the layout manager after the components are added and the
container is already made visible, the components appear as if they have been
added by the previous layout manager (if none was associated before, then the
default). Only subsequent operations (such as resizing) on the container use the
new layout manager. But if the container was not made visible before the new
layout is added, the components are re-laid out by the new layout manager.
· Positioning can be done
manually by passing null to setLayout.
· Flow Layout Manager
§ Honors components preferred
size.(Doesn’t constraint height or width)
§ Arranges components in
horizontal rows, if there’s not enough space, it creates another row.
§ If the container is not big
enough to show all the components, Flow Layout Manager does not resize the
component, it just displays whatever can be displayed in the space the container
has.
§ Justification (LEFT, RIGHT or
CENTER) can be specified in the constructor of layout manager.
§ Default for applets and
panels.
· Grid Layout Manager
§ Never honors the components’
preferred size
§ Arranges the components in no
of rows/columns specified in the constructor. Divides the space the container
has into equal size cells that form a matrix of rows/columns.
§ Each component will take up a
cell in the order in which it is added (left to right, row by row)
§ Each component will be of the
same size (as the cell)
§ If a component is added when
the grid is full, a new column is created and the entire container is re-laid
out.
· Border Layout Manager
§ Divides the container into 5
regions – NORTH, SOUTH, EAST, WEST and CENTER
§ When adding a component,
specify which region to add. If nothing is specified, CENTER is assumed by
default.
§ Regions can be specified by
the constant strings defined in BorderLayout (all upper case) or using Strings
(Title case, like North, South etc)
§ NORTH and SOUTH components –
height honored, but made as wide as the container. Used for toolbars and status
bars.
§ EAST and WEST components –
width honored, but made as tall as the container (after the space taken by
NORTH, SOUTH components). Used for scrollbars.
§ CENTER takes up the left over
space. If there are no other components, it gets all the space.
§ If no component is added to
CENTER, container’s background color is painted in that space.
§ Each region can display only
one component. If another component is added, it hides the earlier component.
· Card Layout Manager
§ Draws in time rather than
space. Only one component displayed at a time.
§ Like a tabbed panel without
tabs. (Can be used for wizards interface, i.e. by clicking next, displays the
next component)
§ Components added are given a
name and methods on the CardLayout manager can be invoked to show the component
using this name. Also the manager contains methods to iterate through the
components. For all methods, the parent container should be specified.
first(Container parent)
next(Container parent)
previous(Container parent)
last(Container parent)
show(Container parent, String name)
§ Component shown occupies the
entire container. If it is smaller it is resized to fit the entire size of the
container. No visual clue is given about the container has other components.
· Gridbag Layout Manager
§ Like the GridLayout manger
uses a rectangular grid.
§ Flexible. Components can
occupy multiple cells. Also the width and height of the cells need not be
uniform. i.e A component may span multiple rows and columns but the region it
occupies is always rectangular. Components can have different sizes (which is
not the case with Grid layout)
§ Requires lot of constraints
to be set for each component that is added.
§ GridBagConstraints class is
used to specify the constraints.
§ Same GridBagConstraints
object can be re-used by all the components.
|
|
Name of the
constraints |
Description |
Default |
|
Location |
int gridx int gridy |
Column and
row positions of the upper left corner of the component in the grid. Added
relative to the previous component if specified
GridBagConstraints.RELATIVE |
GridBagConstraints.RELATIVE in both directions |
|
Dimension |
int
gridwidth int
gridheight |
Number of
cells occupied by the component horizontally and vertically in the grid.
GridBagConstraints.REMAINDER
- specify this for last component
GridBagConstraints.RELATIVE
- specify this for next-to-last component |
One cell in
both directions |
|
Growth
Factor |
double
weigthx double
weigthy |
How to use
the extra space if available. |
0 for both,
meaning that the area allocated for the component will not grow beyond the
preferred size. |
|
Anchoring |
int anchor |
Where a
component should be placed within its display area. Constants
defined in GridBagConstraints: CENTER,
NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST |
GridBagConstraints.CENTER |
|
Filling |
int fill |
How the
component is to stretch and fill its display area. Constants
defined in GridBagConstraints: NONE, BOTH,
HORIZONTAL, VERTICAL |
GridBagConstraints.NONE |
|
Padding |
int ipadx int ipady |
Internal
padding added to each side of the component. Dimension of the component
will grow to (width + 2 * ipadx) and (height + 2 * ipady) |
0 pixels in
either direction |
|
Insets |
Insets
insets |
External
padding (border) around the component. |
(0,0,0,0)
(top, left, bottom, right) |
|
Layout
Manager |
Description |
Constructors |
Constants |
Default For |
|
FlowLayout |
Lays out the
components in row-major order. Rows growing from left to right, top to
bottom. |
FlowLayout() - center aligned, 5 pixels gap both horizontally and
vertically
FlowLayout(int alignment)
FlowLayout(int alignment, int hgap, int vgap) |
LEFT CENTER RIGHT |
Panel and
its subclasses (Applet) |
|
GridLayout |
Lays out the
components in a specified rectangular grid, from left to right in each row
and filling rows from top to bottom. |
GridLayout()
– equivalent to GridLayout(1,0)
GridLayout(int rows, int columns) |
N/A |
None |
|
BorderLayout |
Up to 5
components can be placed in particular locations: north, south, east, west
and center. |
BorderLayout()
BorderLayout(int hgap, int vgap) |
NORTH SOUTH EAST WEST CENTER |
Window and
its subclasses (Dialog and Frame) |
|
CardLayout |
Components
are handled as a stack of indexed cards. Shows only one at a time. |
CardLayout()
CardLayout(int hgap, int vgap) |
N/A |
None |
|
GridbagLayout |
Customizable
and flexible layout manager that lays out the components in a rectangular
grid. |
GridbagLayout() |
Defined in
GridBag Constraints
class. See the above table |
None |
