Java Reference
In-Depth Information
Grid Bag Layout
The last of the layout managers available through Java is grid bag layout, a complex
extension of the grid layout manager. A grid bag layout differs from grid layout in the
following ways:
A component can take up more than one cell in the grid.
n
The proportions between different rows and columns do not have to be equal.
n
A component does not have to fill the entire cell (or cells) that it occupies.
n
A component can be aligned along any edge of a cell.
n
A grid bag layout requires the GridBagLayout and GridBagConstraints classes, which
both are part of the java.awt package. GridBagLayout is the layout manager, and
GridBagConstraints defines the placement of components in the grid.
The constructor for the grid bag layout manager takes no arguments and can be applied
to a container like any other manager. The following statements could be used in a frame
or window's constructor method to use grid bag layout in that container:
Container pane = getContentPane();
GridBagLayout bag = new GridBagLayout();
pane.setLayout(bag);
11
In a grid bag layout, each component uses a GridBagConstraints object to dictate the
cell or cells that it occupies in the grid, its size, and other aspects of its presentation.
A GridBagConstraints object has 11 instance variables that determine component
placement:
gridx —The x position of the cell that holds the component (if it spans several
cells, the x position of the upper-left portion of the component)
n
gridy —The y position of the cell or its upper-left portion
n
gridwidth —The number of cells the component occupies in a horizontal direction
n
gridheight —The number of cells the component occupies in a vertical direction
n
weightx —A value that indicates the component's size relative to other components
on the same row of the grid
n
weighty —A value that indicates its size relative to components on the same grid
column
n
anchor —A value that determines where the component is displayed within its cell
(if it doesn't fill the entire cell)
n
fill —A value that determines whether the component expands horizontally or
vertically to fill its cell
n
 
Search WWH ::




Custom Search