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
|| int gridx
|Column and row positions of the upper left corner of
the component in the grid. Added relative to the previous component if
||GridBagConstraints.RELATIVE in both directions
|| int gridwidth
| Number of cells occupied by the component horizontally
and vertically in the grid.
-specify this for last component
-specify this for next-to-last component
|One cell in both directions
|| double weigthx
|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.
|| Where a component should be placed within its display
Constants defined in
CENTER, NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH,
SOUTHWEST, WEST, NORTHWEST
|| How the component is to stretch and fill its display
Constants defined in
NONE, BOTH, HORIZONTAL, VERTICAL
|| int ipadx
|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
||External padding (border) around the component.
||(0,0,0,0) (top, left, bottom, right)
||Lays out the components in row-major order. Rows
growing from left to right, top to bottom.
center aligned, 5 pixels gap both horizontally and vertically
FlowLayout(int alignment, int
hgap, int vgap)
|Panel and its subclasses (Applet)
||Lays out the components in a specified rectangular
grid, from left to right in each row and filling rows from top to
GridLayout()- equivalent to GridLayout(1,0)
GridLayout(int rows, int columns)
||Up to 5 components can be placed in particular
locations: north, south, east, west and center.
hgap, int vgap)
|Window and its subclasses (Dialog and Frame)
||Components are handled as a stack of indexed cards.
Shows only one at a time.
hgap, int vgap)
||Customizable and flexible layout manager that lays out
the components in a rectangular grid.
DDefined in GridBag
Constraints class. See the above table