Customizing the layout of components in GridBagLayout

You can use the Customize Layout window to specify anchor position, insets, fill, span, padding, and weight for components within GridBagLayout.

In GridBagLayout, the visual editor uses default values for constraints, such as fill, anchor, weight x, and weight y, in order to display the component better. For example, when you drop a javax.swing.JTextField, the fill is set to HORIZONTAL (default value for JTextField is NONE), and the weight X value is set to 1 (default for JTextField value is 0).

When switching to a GridBagLayout from another layout manager, the visual editor for Java generates constraints for each component based on their existing bounds. An advantage of using GridBagConstraints instead of a null layout is that each component is sized according to its preferred size. So, if the size of the parent window changes or if the preferred sizes change at run time due to different language strings, the layout manager will resize and reposition all of the components and make the best use of the available space.

To customize the layout of components within GridBagLayout:

  1. Select the component that you want to customize within the GridBagLayout, and click the Customize Layout Customize Layout toolbar button toolbar button
    Tip: Select multiple components to set identical anchor, fill, and inset constraints on multiple components at the same time.
    The Customize Layout window shows the anchor position, insets, fill, span, padding, and weight values of the selected components on the GridBagLayout. If multiple components are selected, the anchor value and fill values only show a pressed button if all the components have the same value. The inset values shown are those set for the last component selected.

    Customize Layout window

  2. To adjust the anchor position of the component, click one of the compass buttons:
    • Anchor northwest - Positions the component in the upper-left corner of the grid cell.
    • Anchor north - Positions the component in the top center of the grid cell.
    • Anchor northeast - Positions the component in the upper-right corner of the grid cell.
    • Anchor west - Positions the component in the left middle of the grid cell.
    • Anchor center - Positions the component in the center of the grid cell.
    • Anchor east - Positions the component in the right middle of the grid cell.
    • Anchor southwest - Positions the component in the lower-left corner of the grid cell.
    • Anchor south - Positions the component in the bottom center of the grid cell.
    • Anchor southeast - Positions the component in the lower-right corner of the grid cell.
  3. To adjust the padding between the grid cell border and the component, enter a value (in pixels) for any of the following fields:
    • Top - Sets the inset, or padding, above the component.
    • Left - Sets the inset, or padding, to the left of the component.
    • Bottom - Sets the inset, or padding, below the component.
    • Right - Sets the inset, or padding, to the right of the component.
  4. To adjust how much the component fills the grid cell, click either or both of the fill buttons:
    • Fill horizontal - Specifies that the component should occupy the full width of the grid cell.
    • Fill vertical - Specifies that the component should occupy the full height of the grid cell.
  5. To specify how many cells a component should span, enter values for Width and Height:
    • Width - Specifies the number of columns the component occupies (the X axis).
    • Height - Specifies the number of rows the component occupies (the Y axis).
  6. To specify internal padding for a component, enter values (in pixels) for the following fields:
    • X -
    • Y -
  7. To specify how to distribute extra space across rows or columns, enter values (in relative numerical values, in relation to the weights specified for other components) for the X and Y fields.

    The GridBagLayout manager calculates the weight of columns and rows to be the maximum weightx and weighty of all the components in a row and column, respectively. If the resulting layout is smaller than the area it needs to fill, the extra space in the layout will be distributed to the columns and rows in proportion to the weights specified. A cell that has a weight of zero receives no extra space.

    For example, if a component in column 1, row 1 has a weight X of 3, and a component in column 2, row 1 has a weight X of 1, the total of the weight X values in row 1 add up to 4, and column 1 will receive 75% of the extra space when space is distributed.

    • X - Specifies the weight for distribution of extra space between columns.
    • Y - Specifies the weight for distribution of extra space between rows.
Related tasks
Adding or moving components within GridBagLayout
Spanning components across GridBagLayout cells

(C) Copyright IBM Corporation 1999, 2004. All Rights Reserved.