GridBagConstraints


anchor

Description:

When the component is smaller than its display area, use the anchor constraint to tell the layout manager where to place the component within the area.

The anchor constraint only affects the component within its own display area, depending on the fill constraint for the component. For example, if the fill constraint value for a component is GridBagConstraints.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 GridBagConstraints.NONE, GridBagConstraints.HORIZONTAL, or GridBagConstraints.VERTICAL

Valid values:
   
GridBagConstraints.CENTER
GridBagConstraints.NORTH
GridBagConstraints.NORTHEAST
GridBagConstraints.EAST
GridBagConstraints.SOUTHEAST
GridBagConstraints.SOUTH
GridBagConstraints.SOUTHWEST
GridBagConstraints.WEST
GridBagConstraints.NORTHWEST
Default value:
   
GridBagConstraints.CENTER

Tips on setting anchor constraints


fill

Description:

When the component's display area is larger than the component's requested size, use the fill constraint to tell the layout manager which parts of the display area should be given to the component.

As with the anchor constraint, the fill constraint only affects the component within its own display area. Fill tells the layout manager to expand the component to fill the whole area it has been given.

Valid values:
   
GridBagConstraints.NONE (Don't change the size of the component.)
   
GridBagConstraints.BOTH (Resize the component both horizontally and vertically to fill the area completely.)
   
GridBagConstraints.HORIZONTAL (Only resize the component to fill the area horizontally.)
   
GridBagConstraints.VERTICAL (Only resize the component to fill the area vertically.)
Default value:
   
GridBagConstraints.NONE

Tips on setting fill constraints


insets

Description:

Use insets to specify the minimum amount of external space (padding) in pixels between the component and the edges of its display area. The inset says that there must always be the specified gap between the edge of the component and the corresponding edge of the cell. Therefore, insets work like brakes on the component to keep it away from the edges of the cell. For example, if you increase the width of a component with left and right insets to be wider than its cell, the cell will expand to accommodate the component plus its insets. Because of this, fill and padding constraints never steal any space from insets.

Valid values:
insets = new Insets(n,n,n,n)
Top, left, bottom, right (where each parameter represents the number of pixels between the display area and one edge of the cell.)
Default values:
insets = new Insets(0,0,0,0)

Tips on setting insets constraints


gridwidth, gridheight

Description:

Use gridwidth and gridheight constraints to specify the number of cells in a row (gridwidth) or column (gridheight) the component uses. This constraint value is stated in cell numbers, not in pixels.

Valid values:
   
gridwidth=nn, gridheight=nn
 
(Where nn is an integer representing the number of cell columns or rows.)
   
GridBagConstraints.RELATIVE (-1)
 
Specifies that this component is the next to last one in the row (gridwidth) or column (gridheight.) A component with a GridBagConstraints.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.
   
GridBagConstraints.REMAINDER (0)
 
Specifies that this component is the last one in the row (gridwidth) or column (gridheight).
Default value:
   
gridwidth=1, gridheight=1

Tips on setting gridwidth, gridheight constraints


ipadx, ipady

Description:

These constraints specify the internal padding for a component:

Use ipadx and ipady to specify the amount of space (in pixels) to add to the minimum size of the component for internal padding. For example, the width of the component will be at least its minimum width plus ipadx in pixels. The code only adds it once, splitting it evenly between both sides of the component. Similarly, the height of the component will be at least the minimum height plus ipady pixels.

Example:

When added to a component that has a preferred size of 30 pixels wide and 20 pixels high:

Valid values:
   
ipadx=nn, ipadx=nn
Default value:
   
ipadx=0, ipady=0

Tips on setting ipadx, ipady constraints


gridx, gridy

Description:

Use these constraints to specify the grid cell location for the upper left corner of component. For example, 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).

GridBagConstraints.RELATIVE specifies that the component be placed relative to the previous component as follows:

Valid values:
   
gridx=nn, gridy=nn
GridBagConstraints.RELATIVE (-1)
Default value:
   
gridx=1, gridy=1

Tips on setting gridx,gridy constraints


weightx, weighty

Description:

Use the weight constraints to specify how to distribute a GridBagLayout container's extra space horizontally (weightx) and vertically (weighty) when the container is resized. Weights determine what share of the extra space gets allocated to each cell and component when the container is enlarged beyond its default size.

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.

Note  Important: If you want your cells to grow, weightx and weighty must be set to a non-zero value.

Valid values:
   
weightx=n.n, weighty=n.n
Default value:
   
weightx=0.0, weighty=0.0

Tips on setting weightx, weighty constraints