Layout Managers
setLayout(new LayoutManager())
if null provided as parameter then absolute positioning used
--use a component's setBounds method to set its size and position
BorderLayout: Good top-level manager for laying out a application window
1) 5 regions corresponding to N, S, E, W, and center. Called
BorderLayout.{PAGE_START, PAGE_END, LINE_START, LINE_END, CENTER} by Java
2) N/S regions go completely across the screen
3) On resize
a) the center gets all the available space.
b) the N/S regions resize their width
c) the E/W regions reside their height
4) is a variation of the edge-anchored algorithm
5) sample code for border layout
FlowLayout: Good for simple left-to-right layout of items with their natural,
preferred sizes
1) align property aligns objects with respect to the enclosing container
a) left/right/center: obvious thing with respect to container
b) leading/trailing: depends on whether orientation is left-to-right
or right-to-left. For example, for right-to-left, the leading will
cause the items to be right-justified and "trailing" will cause the
items to be left-justified
2) can specify horizontal/vertical spacing
3) spills objects into multiple rows if window is not big enough to
accommodate all the objects
4) sample code for flow layout
GridLayout: Good for organizing items into a table where each component has
same size
1) largest item determines size of cells: maximum size preferences will
be violated and items will be made large enough to accommodate the
cell size
2) can specify horizontal/vertical spaces between cells
3) can specify 0 for either row or col but not both. As an example,
"new GridLayout(0,2)" specifies two columns and an unlimited number
of rows
4) when a container is assigned space, the cells are made large enough to
exactly fill the container--maximum size is ignored
5) the grid layout manager does not satisfy minimum
size requests
6) sample code for grid layout
BoxLayout: Good for organizing stacks of items. Often times you will combine
box layouts to achieve a desired effect.
1) vertical/horizontal layout (BoxLayout.LINE_AXIS or PAGE_AXIS)
2) alignment within cell determined by a component's XAlignment and
YAlignment properties
a) component.setAlignmentX/setAlignmentY(float between 0 and 1)
b) ease-of-use constants: Component.{BOTTOM,CENTER,LEFT,RIGHT,TOP}_ALIGNMENT
c) there is no way to specify in a single place that all components in
the box should be left-justified, centered, or right-justified. You
need to set each component's alignment fields separately
d) it is a good idea to make all the components have the same alignment.
Many alignment problems are caused by components having differing
alignments. If your box layout looks strange, one of the first
things to check is whether or not the components all have the same
alignment
3) takes account of min/preferred/max size
a) if a container gets too much space then the items will never be made
larger than their maximum size
b) if a component specifies differing preferred and maximum sizes, then
if the container becomes bigger than the component's preferred size,
the component will start expanding until it reaches its maximum size.
4) can use Box class to get a horizontal or vertical box container
a) Box.createHorizontalBox
b) Box.createVerticalBox
5) rigid area/glue can control spacing between objects
a) Box.createRigidArea(Dimension): creates a fixed space between objects
b) Box.createHorizontalGlue: absorbs excess horizontal space
c) Box.createVerticalGlue: absorbs excess vertical space
d) excess space is spread evenly among glue objects. If you wanted an
object to appear 2/3 of the way toward the right side of the container,
you would add 2 HorizontalGlue objects to the left of the component and
one HorizontalGlue object to the right of the component
e) instead of using glue objects, you can frequently control a
container's alignment using the setAlignmentX and setAlignmentY
properties
6) do not use horizontal or vertical struts: they are deprecated and they
do not bound their other dimension. The failure to bound the other
dimension can cause this dimension to receive a large amount of area and
cause the container to get unexpectedly large
7) sample box programs
a) BoxLayoutDemo.java: A simple box
layout with a vertical stack
b) FlowBoxLayoutDemo.java: The flow
layout example from earlier but now the radio buttons and "Apply
Application" button are laid out using a series of boxes.
GridBagLayout: Good for organizing forms
1) GridBagConstraints: object that holds constraints
a) usually allocate a new object for each component
2) container.add(component, constraints)
3) properties
a) gridx, gridy: the row and column
b) gridwidth, gridheight: width and height in terms of cells
c) fill: none, vertical, horizontal, both: how to size the component
if it is not big enough to fit its cell
d) ipadx, ipady: internal padding--added inside the component's border
component wd = minwidth + 2*ipadx
e) insets: external padding of the component: space between component's
border and edges of its display area
f) anchor: Used when component is smaller than its display area:
-------------------------------------------------
|FIRST_LINE_START PAGE_START FIRST_LINE_END|
| |
| |
|LINE_START CENTER LINE_END|
| |
| |
|LAST_LINE_START PAGE_END LAST_LINE_END|
-------------------------------------------------
g) weightx, weighty: used to specify how columns/rows get extra
space.
i) values should be between 0.0 and 1.0
ii) a row's weight is related to the highest weighty specified for
any component in that row. The same is true for a column.
iii) it is an art to specify the weightx and weighty to get the right
behavior. Often you must play with it to get what you want.
4) sample code for grid bag layout
Debugging:
1) When debugging a non-graphical program you typically use print
statements.
2) When debugging layout managers you can also draw borders around containers
to determine their extents.
a) You should not assume that a container begins where its first component
begins and ends where its last component ends. For example, if a box
layout has horizontal glue before or after the last component, then
the container may extend beyond the first or last component.
b) Use the LineBorder class to draw your borders. Here's an example:
component.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
A LineBorder takes a color and a line size.