Java Reference
In-Depth Information
GridBagLayout Rows and Columns
To help you visualize the grid of components in the GridBagLayout , Figure 10-6 indicates how
the layout manager counts cells. The top-left cell in the layout has location (0, 0). There's nothing
surprising about buttons one, two, three, six, and seven. Each of those occupies a 1×1 area of
the layout's 3×3 grid. Button four occupies a 2×1 area; it is placed at location (0, 1), and thus
occupies this cell plus the cell at (1, 1). Likewise, button five occupies a 1×2 area and takes up
the cells at (2, 1) and (2, 2). The total size of the layout is determined entirely by the components
that are placed in it and their constraints.
Figure 10-6. How GridBagLayout counts rows and columns
GridBagConstraints Class
The magic of the layout manager is strictly controlled by the different GridBagConstraints
objects passed in for each component added to the container. Each specifies how to display a
specific component. Unlike most other layout managers, which have a built-in idea about
what to do with their display, the GridBagLayout is a blank slate. The constraints attached to the
components tell the layout manager how to build its display.
Every component added to a GridBagLayout container should have a GridBagConstraints
object associated with it. When an object is first added to the layout, it is given a default set
of constraints (see Table 10-2). Calling container.add(Component, GridBagConstraints) or
gridBagLayout.setConstraints(GridBagConstraints) applies the new set of constraints to the
component.
GridBagConstraints has two constructors:
public GridBagConstraints()
public GridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight,
double weightx, double weighty, int anchor, int fill, Insets insets, int ipadx,
int ipady)
Search WWH ::




Custom Search