When you use the UI Designer to create a GridBagLayout container, JBuilder automatically instantiates a GridBagConstraints object for each component you add to the container. It uses default constraint values, and/or values calculated about existing components during conversion from another layout (like XYLayout) to GridBagLayout. The Constraints property editor allows you to edit these constraints from the Inspector. Each constraint in the property editor directly corresponds to a constraint from the GridBagConstraints.class as specified below.
gridx=0
is the first column on the left, and gridy=0
is the first row at the top. Therefore, a component with the constraints gridx=0
and gridy=0
is placed in the first cell of the grid (top left).
There is an additional constraint value that can be used for gridx and gridy called RELATIVE
(the default value). RELATIVE specifies that the component be placed relative to the previous component as follows:
To specify a RELATIVE
value for Grid Position X or Y in the Constraints property editor, enter -1
.
There are two additional constraint values that can be used for gridwidth and gridheight: RELATIVE
and REMAINDER
.
RELATIVE
specifies that this component is the next to last one in the row (gridwidth) or column (gridheight). A component using RELATIVE
takes all the remaining cells except the last one. For example, in a row of six columns, if the component starts in the third column, a gridwidth of RELATIVE
will make it take up columns three, four, and five.
REMAINDER
specifies that this component is the last one in the row (gridwidth) or column (gridheight
RELATIVE
value for Grid Position Width or Height in the Constraints property editor, enter -1
.
REMAINDER
value for Grid Position Width or Height in the Constraints property editor, enter 0
.
The width of the component will be at least its minimum width plus ipadx in pixels. (In spite of the fact that the Javadoc comments say ipadx*2, the actual code only adds it once.) Similarly, the height of the component will be at least the minimum height plus ipady pixels.
Example:
When added to a component that is 30 pixels wide and 20 pixels high:
For example, if you have three components with weights of 0.0, 0.3, and 0.2 respectively, when the container is enlarged, none of the extra space will go to the first component, 3/5 of it will go the second component, and 2/5 of it will go to the third.
Weight values are of type double
and are specified numerically in the range 0.0 to 1.0 inclusive. Zero means the component should not receive any of the extra space, and 1.0 means component gets a full share of the space.
For example, if the fill constraint value for a component is BOTH
(fill the display area both horizontally and vertically), the anchor constraint has no effect because the component takes up the entire available area. For the anchor constraint to have an effect, set the fill constraint value to NONE
, HORIZONTAL
, or VERTICAL
.
The values for Anchor are as follows:
NW (NORTHWEST) | N (NORTH) | NE (NORTHEAST) |
W (WEST) | C (CENTER) | E (EAST) |
SW (SOUTHWEST) | S (SOUTH) | SE (SOUTHEAST) |
The values for Fill are as follows:
None | Don't change the size of the component |
Horizontal | Only resize the component to fill the area horizontally. |
Vertical | Only resize the component to fill the area vertically. |
Both | Resize the component both horizontally and vertically to fill the area completely. |
For more information on GridBagLayout and its constraints, see GridBagLayout in the User's Guide.