< prev index next >

src/java.desktop/share/classes/java/awt/GridBagLayout.java

Print this page




 105  * <dt>{@link GridBagConstraints#ipadx},
 106  * {@link GridBagConstraints#ipady}
 107  * <dd>Specifies the component's internal padding within the layout,
 108  * how much to add to the minimum size of the component.
 109  * The width of the component will be at least its minimum width
 110  * plus {@code ipadx} pixels. Similarly, the height of
 111  * the component will be at least the minimum height plus
 112  * {@code ipady} pixels.
 113  * <dt>{@link GridBagConstraints#insets}
 114  * <dd>Specifies the component's external padding, the minimum
 115  * amount of space between the component and the edges of its display area.
 116  * <dt>{@link GridBagConstraints#anchor}
 117  * <dd>Specifies where the component should be positioned in its display area.
 118  * There are three kinds of possible values: absolute, orientation-relative,
 119  * and baseline-relative
 120  * Orientation relative values are interpreted relative to the container's
 121  * {@code ComponentOrientation} property while absolute values
 122  * are not.  Baseline relative values are calculated relative to the
 123  * baseline.  Valid values are:
 124  *
 125  * <center><table BORDER=0 style="width:800"
 126  *        SUMMARY="absolute, relative and baseline values as described above">


 127  * <tr>
 128  * <th><P style="text-align:left">Absolute Values</th>
 129  * <th><P style="text-align:left">Orientation Relative Values</th>
 130  * <th><P style="text-align:left">Baseline Relative Values</th>
 131  * </tr>


 132  * <tr>
 133  * <td>
 134  * <ul style="list-style-type:none">
 135  * <li>{@code GridBagConstraints.NORTH}</li>
 136  * <li>{@code GridBagConstraints.SOUTH}</li>
 137  * <li>{@code GridBagConstraints.WEST}</li>
 138  * <li>{@code GridBagConstraints.EAST}</li>
 139  * <li>{@code GridBagConstraints.NORTHWEST}</li>
 140  * <li>{@code GridBagConstraints.NORTHEAST}</li>
 141  * <li>{@code GridBagConstraints.SOUTHWEST}</li>
 142  * <li>{@code GridBagConstraints.SOUTHEAST}</li>
 143  * <li>{@code GridBagConstraints.CENTER} (the default)</li>
 144  * </ul>
 145  * </td>
 146  * <td>
 147  * <ul style="list-style-type:none">
 148  * <li>{@code GridBagConstraints.PAGE_START}</li>
 149  * <li>{@code GridBagConstraints.PAGE_END}</li>
 150  * <li>{@code GridBagConstraints.LINE_START}</li>
 151  * <li>{@code GridBagConstraints.LINE_END}</li>
 152  * <li>{@code GridBagConstraints.FIRST_LINE_START}</li>
 153  * <li>{@code GridBagConstraints.FIRST_LINE_END}</li>
 154  * <li>{@code GridBagConstraints.LAST_LINE_START}</li>
 155  * <li>{@code GridBagConstraints.LAST_LINE_END}</li>
 156  * </ul>
 157  * </td>
 158  * <td>
 159  * <ul style="list-style-type:none">
 160  * <li>{@code GridBagConstraints.BASELINE}</li>
 161  * <li>{@code GridBagConstraints.BASELINE_LEADING}</li>
 162  * <li>{@code GridBagConstraints.BASELINE_TRAILING}</li>
 163  * <li>{@code GridBagConstraints.ABOVE_BASELINE}</li>
 164  * <li>{@code GridBagConstraints.ABOVE_BASELINE_LEADING}</li>
 165  * <li>{@code GridBagConstraints.ABOVE_BASELINE_TRAILING}</li>
 166  * <li>{@code GridBagConstraints.BELOW_BASELINE}</li>
 167  * <li>{@code GridBagConstraints.BELOW_BASELINE_LEADING}</li>
 168  * <li>{@code GridBagConstraints.BELOW_BASELINE_TRAILING}</li>
 169  * </ul>
 170  * </td>
 171  * </tr>
 172  * </table></center>

 173  * <dt>{@link GridBagConstraints#weightx},
 174  * {@link GridBagConstraints#weighty}
 175  * <dd>Used to determine how to distribute space, which is
 176  * important for specifying resizing behavior.
 177  * Unless you specify a weight for at least one component
 178  * in a row ({@code weightx}) and column ({@code weighty}),
 179  * all the components clump together in the center of their container.
 180  * This is because when the weight is zero (the default),
 181  * the {@code GridBagLayout} object puts any extra space
 182  * between its grid of cells and the edges of the container.
 183  * </dl>
 184  * <p>
 185  * Each row may have a baseline; the baseline is determined by the
 186  * components in that row that have a valid baseline and are aligned
 187  * along the baseline (the component's anchor value is one of {@code
 188  * BASELINE}, {@code BASELINE_LEADING} or {@code BASELINE_TRAILING}).
 189  * If none of the components in the row has a valid baseline, the row
 190  * does not have a baseline.
 191  * <p>
 192  * If a component spans rows it is aligned either to the baseline of
 193  * the start row (if the baseline-resize behavior is {@code
 194  * CONSTANT_ASCENT}) or the end row (if the baseline-resize behavior
 195  * is {@code CONSTANT_DESCENT}).  The row that the component is
 196  * aligned to is called the <em>prevailing row</em>.
 197  * <p>
 198  * The following figure shows a baseline layout and includes a
 199  * component that spans rows:
 200  * <center><table summary="Baseline Layout">

 201  * <tr style="text-align:center">
 202  * <td>
 203  * <img src="doc-files/GridBagLayout-baseline.png"
 204  *  alt="The following text describes this graphic (Figure 1)." style="float:center">
 205  * </td>
 206  * </table></center>
 207  * This layout consists of three components:
 208  * <ul><li>A panel that starts in row 0 and ends in row 1.  The panel
 209  *   has a baseline-resize behavior of {@code CONSTANT_DESCENT} and has
 210  *   an anchor of {@code BASELINE}.  As the baseline-resize behavior
 211  *   is {@code CONSTANT_DESCENT} the prevailing row for the panel is
 212  *   row 1.
 213  * <li>Two buttons, each with a baseline-resize behavior of
 214  *   {@code CENTER_OFFSET} and an anchor of {@code BASELINE}.
 215  * </ul>
 216  * Because the second button and the panel share the same prevailing row,
 217  * they are both aligned along their baseline.
 218  * <p>
 219  * Components positioned using one of the baseline-relative values resize
 220  * differently than when positioned using an absolute or orientation-relative
 221  * value.  How components change is dictated by how the baseline of the
 222  * prevailing row changes.  The baseline is anchored to the
 223  * bottom of the display area if any components with the same prevailing row
 224  * have a baseline-resize behavior of {@code CONSTANT_DESCENT},
 225  * otherwise the baseline is anchored to the top of the display area.
 226  * The following rules dictate the resize behavior:


 235  * <li>Resizable components positioned on the baseline with a
 236  * baseline-resize behavior of {@code OTHER} are only resized if
 237  * the baseline at the resized size fits within the display area.  If
 238  * the baseline is such that it does not fit within the display area
 239  * the component is not resized.
 240  * <li>Components positioned on the baseline that do not have a
 241  * baseline-resize behavior of {@code OTHER}
 242  * can only grow as tall as {@code display height - baseline + baseline of component}.
 243  * </ul>
 244  * If you position a component along the baseline, but the
 245  * component does not have a valid baseline, it will be vertically centered
 246  * in its space.  Similarly if you have positioned a component relative
 247  * to the baseline and none of the components in the row have a valid
 248  * baseline the component is vertically centered.
 249  * <p>
 250  * The following figures show ten components (all buttons)
 251  * managed by a grid bag layout.  Figure 2 shows the layout for a horizontal,
 252  * left-to-right container and Figure 3 shows the layout for a horizontal,
 253  * right-to-left container.
 254  *
 255  * <center><table style="width:600" summary="layout">

 256  * <tr style="text-align:center">
 257  * <td>
 258  * <img src="doc-files/GridBagLayout-1.gif" alt="The preceding text describes this graphic (Figure 1)." style="float:center; margin: 7px 10px;">
 259  * </td>
 260  * <td>
 261  * <img src="doc-files/GridBagLayout-2.gif" alt="The preceding text describes this graphic (Figure 2)." style="float:center; margin: 7px 10px;">
 262  * </td>
 263  * <tr style="text-align:center">
 264  * <td>Figure 2: Horizontal, Left-to-Right</td>
 265  * <td>Figure 3: Horizontal, Right-to-Left</td>
 266  * </tr>
 267  * </table></center>
 268  * <p>
 269  * Each of the ten components has the {@code fill} field
 270  * of its associated {@code GridBagConstraints} object
 271  * set to {@code GridBagConstraints.BOTH}.
 272  * In addition, the components have the following non-default constraints:
 273  *
 274  * <ul>
 275  * <li>Button1, Button2, Button3: <code>weightx&nbsp;=&nbsp;1.0</code>
 276  * <li>Button4: <code>weightx&nbsp;=&nbsp;1.0</code>,
 277  * <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 278  * <li>Button5: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 279  * <li>Button6: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.RELATIVE</code>
 280  * <li>Button7: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 281  * <li>Button8: <code>gridheight&nbsp;=&nbsp;2</code>,
 282  * <code>weighty&nbsp;=&nbsp;1.0</code>
 283  * <li>Button9, Button 10:
 284  * <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 285  * </ul>
 286  * <p>
 287  * Here is the code that implements the example shown above:




 105  * <dt>{@link GridBagConstraints#ipadx},
 106  * {@link GridBagConstraints#ipady}
 107  * <dd>Specifies the component's internal padding within the layout,
 108  * how much to add to the minimum size of the component.
 109  * The width of the component will be at least its minimum width
 110  * plus {@code ipadx} pixels. Similarly, the height of
 111  * the component will be at least the minimum height plus
 112  * {@code ipady} pixels.
 113  * <dt>{@link GridBagConstraints#insets}
 114  * <dd>Specifies the component's external padding, the minimum
 115  * amount of space between the component and the edges of its display area.
 116  * <dt>{@link GridBagConstraints#anchor}
 117  * <dd>Specifies where the component should be positioned in its display area.
 118  * There are three kinds of possible values: absolute, orientation-relative,
 119  * and baseline-relative
 120  * Orientation relative values are interpreted relative to the container's
 121  * {@code ComponentOrientation} property while absolute values
 122  * are not.  Baseline relative values are calculated relative to the
 123  * baseline.  Valid values are:
 124  *
 125  * <table class="striped" style="margin: 0px auto">
 126  * <caption style="display:none">Absolute, relative and baseline values as
 127  * described above</caption>
 128  * <thead>
 129  * <tr>
 130  * <th><p style="text-align:center">Absolute Values</th>
 131  * <th><p style="text-align:center">Orientation Relative Values</th>
 132  * <th><p style="text-align:center">Baseline Relative Values</th>
 133  * </tr>
 134  * </thead>
 135  * <tbody>
 136  * <tr>
 137  * <td>
 138  * <ul style="list-style-type:none">
 139  * <li>{@code GridBagConstraints.NORTH}</li>
 140  * <li>{@code GridBagConstraints.SOUTH}</li>
 141  * <li>{@code GridBagConstraints.WEST}</li>
 142  * <li>{@code GridBagConstraints.EAST}</li>
 143  * <li>{@code GridBagConstraints.NORTHWEST}</li>
 144  * <li>{@code GridBagConstraints.NORTHEAST}</li>
 145  * <li>{@code GridBagConstraints.SOUTHWEST}</li>
 146  * <li>{@code GridBagConstraints.SOUTHEAST}</li>
 147  * <li>{@code GridBagConstraints.CENTER} (the default)</li>
 148  * </ul>
 149  * </td>
 150  * <td>
 151  * <ul style="list-style-type:none">
 152  * <li>{@code GridBagConstraints.PAGE_START}</li>
 153  * <li>{@code GridBagConstraints.PAGE_END}</li>
 154  * <li>{@code GridBagConstraints.LINE_START}</li>
 155  * <li>{@code GridBagConstraints.LINE_END}</li>
 156  * <li>{@code GridBagConstraints.FIRST_LINE_START}</li>
 157  * <li>{@code GridBagConstraints.FIRST_LINE_END}</li>
 158  * <li>{@code GridBagConstraints.LAST_LINE_START}</li>
 159  * <li>{@code GridBagConstraints.LAST_LINE_END}</li>
 160  * </ul>
 161  * </td>
 162  * <td>
 163  * <ul style="list-style-type:none">
 164  * <li>{@code GridBagConstraints.BASELINE}</li>
 165  * <li>{@code GridBagConstraints.BASELINE_LEADING}</li>
 166  * <li>{@code GridBagConstraints.BASELINE_TRAILING}</li>
 167  * <li>{@code GridBagConstraints.ABOVE_BASELINE}</li>
 168  * <li>{@code GridBagConstraints.ABOVE_BASELINE_LEADING}</li>
 169  * <li>{@code GridBagConstraints.ABOVE_BASELINE_TRAILING}</li>
 170  * <li>{@code GridBagConstraints.BELOW_BASELINE}</li>
 171  * <li>{@code GridBagConstraints.BELOW_BASELINE_LEADING}</li>
 172  * <li>{@code GridBagConstraints.BELOW_BASELINE_TRAILING}</li>
 173  * </ul>
 174  * </td>
 175  * </tr>
 176  * </tbody>
 177  * </table>
 178  * <dt>{@link GridBagConstraints#weightx},
 179  * {@link GridBagConstraints#weighty}
 180  * <dd>Used to determine how to distribute space, which is
 181  * important for specifying resizing behavior.
 182  * Unless you specify a weight for at least one component
 183  * in a row ({@code weightx}) and column ({@code weighty}),
 184  * all the components clump together in the center of their container.
 185  * This is because when the weight is zero (the default),
 186  * the {@code GridBagLayout} object puts any extra space
 187  * between its grid of cells and the edges of the container.
 188  * </dl>
 189  * <p>
 190  * Each row may have a baseline; the baseline is determined by the
 191  * components in that row that have a valid baseline and are aligned
 192  * along the baseline (the component's anchor value is one of {@code
 193  * BASELINE}, {@code BASELINE_LEADING} or {@code BASELINE_TRAILING}).
 194  * If none of the components in the row has a valid baseline, the row
 195  * does not have a baseline.
 196  * <p>
 197  * If a component spans rows it is aligned either to the baseline of
 198  * the start row (if the baseline-resize behavior is {@code
 199  * CONSTANT_ASCENT}) or the end row (if the baseline-resize behavior
 200  * is {@code CONSTANT_DESCENT}).  The row that the component is
 201  * aligned to is called the <em>prevailing row</em>.
 202  * <p>
 203  * The following figure shows a baseline layout and includes a
 204  * component that spans rows:
 205  * <table class="borderless" style="margin: 0px auto">
 206  * <caption style="display:none">Baseline Layout</caption>
 207  * <tr style="text-align:center">
 208  * <td>
 209  * <img src="doc-files/GridBagLayout-baseline.png"
 210  *  alt="The following text describes this graphic (Figure 1)." style="float:center">
 211  * </td>
 212  * </table>
 213  * This layout consists of three components:
 214  * <ul><li>A panel that starts in row 0 and ends in row 1.  The panel
 215  *   has a baseline-resize behavior of {@code CONSTANT_DESCENT} and has
 216  *   an anchor of {@code BASELINE}.  As the baseline-resize behavior
 217  *   is {@code CONSTANT_DESCENT} the prevailing row for the panel is
 218  *   row 1.
 219  * <li>Two buttons, each with a baseline-resize behavior of
 220  *   {@code CENTER_OFFSET} and an anchor of {@code BASELINE}.
 221  * </ul>
 222  * Because the second button and the panel share the same prevailing row,
 223  * they are both aligned along their baseline.
 224  * <p>
 225  * Components positioned using one of the baseline-relative values resize
 226  * differently than when positioned using an absolute or orientation-relative
 227  * value.  How components change is dictated by how the baseline of the
 228  * prevailing row changes.  The baseline is anchored to the
 229  * bottom of the display area if any components with the same prevailing row
 230  * have a baseline-resize behavior of {@code CONSTANT_DESCENT},
 231  * otherwise the baseline is anchored to the top of the display area.
 232  * The following rules dictate the resize behavior:


 241  * <li>Resizable components positioned on the baseline with a
 242  * baseline-resize behavior of {@code OTHER} are only resized if
 243  * the baseline at the resized size fits within the display area.  If
 244  * the baseline is such that it does not fit within the display area
 245  * the component is not resized.
 246  * <li>Components positioned on the baseline that do not have a
 247  * baseline-resize behavior of {@code OTHER}
 248  * can only grow as tall as {@code display height - baseline + baseline of component}.
 249  * </ul>
 250  * If you position a component along the baseline, but the
 251  * component does not have a valid baseline, it will be vertically centered
 252  * in its space.  Similarly if you have positioned a component relative
 253  * to the baseline and none of the components in the row have a valid
 254  * baseline the component is vertically centered.
 255  * <p>
 256  * The following figures show ten components (all buttons)
 257  * managed by a grid bag layout.  Figure 2 shows the layout for a horizontal,
 258  * left-to-right container and Figure 3 shows the layout for a horizontal,
 259  * right-to-left container.
 260  *
 261  * <table class="borderless" style="margin: 0px auto">
 262  * <caption style="width:600;display:none">Figures</caption>
 263  * <tr style="text-align:center">
 264  * <td>
 265  * <img src="doc-files/GridBagLayout-1.gif" alt="The preceding text describes this graphic (Figure 1)." style="float:center; margin: 7px 10px;">
 266  * </td>
 267  * <td>
 268  * <img src="doc-files/GridBagLayout-2.gif" alt="The preceding text describes this graphic (Figure 2)." style="float:center; margin: 7px 10px;">
 269  * </td>
 270  * <tr style="text-align:center">
 271  * <td>Figure 2: Horizontal, Left-to-Right</td>
 272  * <td>Figure 3: Horizontal, Right-to-Left</td>
 273  * </tr>
 274  * </table>
 275  * <p>
 276  * Each of the ten components has the {@code fill} field
 277  * of its associated {@code GridBagConstraints} object
 278  * set to {@code GridBagConstraints.BOTH}.
 279  * In addition, the components have the following non-default constraints:
 280  *
 281  * <ul>
 282  * <li>Button1, Button2, Button3: <code>weightx&nbsp;=&nbsp;1.0</code>
 283  * <li>Button4: <code>weightx&nbsp;=&nbsp;1.0</code>,
 284  * <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 285  * <li>Button5: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 286  * <li>Button6: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.RELATIVE</code>
 287  * <li>Button7: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 288  * <li>Button8: <code>gridheight&nbsp;=&nbsp;2</code>,
 289  * <code>weighty&nbsp;=&nbsp;1.0</code>
 290  * <li>Button9, Button 10:
 291  * <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 292  * </ul>
 293  * <p>
 294  * Here is the code that implements the example shown above:


< prev index next >