SCJP Java Programming Exam Cram Notes : layout Managers

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.
Specify 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)

NNORTH
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. CCardLayout()
CardLayout(int hgap, int vgap)

N/A/ None
GridbagLayout Customizable and flexible layout manager that lays out the components in a rectangular grid. GridbagLayout() DDefined in GridBag
Constraints class. See the above table

None

Previous    Contents    Next