< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


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


 240  * <li>Resizable components positioned on the baseline with a
 241  * baseline-resize behavior of {@code OTHER} are only resized if
 242  * the baseline at the resized size fits within the display area.  If
 243  * the baseline is such that it does not fit within the display area
 244  * the component is not resized.
 245  * <li>Components positioned on the baseline that do not have a
 246  * baseline-resize behavior of {@code OTHER}
 247  * can only grow as tall as {@code display height - baseline + baseline of component}.
 248  * </ul>
 249  * If you position a component along the baseline, but the
 250  * component does not have a valid baseline, it will be vertically centered
 251  * in its space.  Similarly if you have positioned a component relative
 252  * to the baseline and none of the components in the row have a valid
 253  * baseline the component is vertically centered.
 254  * <p>
 255  * The following figures show ten components (all buttons)
 256  * managed by a grid bag layout.  Figure 2 shows the layout for a horizontal,
 257  * left-to-right container and Figure 3 shows the layout for a horizontal,
 258  * right-to-left container.
 259  *
 260  * <table class="borderless" style="margin: 0px auto">
 261  * <caption style="width:600;display:none">Figures</caption>
 262  * <tr style="text-align:center">
 263  * <td>
 264  * <img src="doc-files/GridBagLayout-1.gif" alt="The preceding text describes this graphic (Figure 1)." style="float:center; margin: 7px 10px;">
 265  * </td>
 266  * <td>
 267  * <img src="doc-files/GridBagLayout-2.gif" alt="The preceding text describes this graphic (Figure 2)." style="float:center; margin: 7px 10px;">
 268  * </td>
 269  * <tr style="text-align:center">
 270  * <td>Figure 2: Horizontal, Left-to-Right</td>
 271  * <td>Figure 3: Horizontal, Right-to-Left</td>
 272  * </tr>
 273  * </table>

 274  * <p>
 275  * Each of the ten components has the {@code fill} field
 276  * of its associated {@code GridBagConstraints} object
 277  * set to {@code GridBagConstraints.BOTH}.
 278  * In addition, the components have the following non-default constraints:
 279  *
 280  * <ul>
 281  * <li>Button1, Button2, Button3: <code>weightx&nbsp;=&nbsp;1.0</code>
 282  * <li>Button4: <code>weightx&nbsp;=&nbsp;1.0</code>,
 283  * <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 284  * <li>Button5: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 285  * <li>Button6: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.RELATIVE</code>
 286  * <li>Button7: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 287  * <li>Button8: <code>gridheight&nbsp;=&nbsp;2</code>,
 288  * <code>weighty&nbsp;=&nbsp;1.0</code>
 289  * <li>Button9, Button 10:
 290  * <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 291  * </ul>
 292  * <p>
 293  * Here is the code that implements the example shown above:


   1 /*
   2  * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  99  * horizontally, but don't change its height),
 100  * {@code GridBagConstraints.VERTICAL}
 101  * (make the component tall enough to fill its display area
 102  * vertically, but don't change its width), and
 103  * {@code GridBagConstraints.BOTH}
 104  * (make the component fill its display area entirely).
 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  * <p>Absolute Values:
 126  * <ul>











 127  * <li>{@code GridBagConstraints.NORTH}</li>
 128  * <li>{@code GridBagConstraints.SOUTH}</li>
 129  * <li>{@code GridBagConstraints.WEST}</li>
 130  * <li>{@code GridBagConstraints.EAST}</li>
 131  * <li>{@code GridBagConstraints.NORTHWEST}</li>
 132  * <li>{@code GridBagConstraints.NORTHEAST}</li>
 133  * <li>{@code GridBagConstraints.SOUTHWEST}</li>
 134  * <li>{@code GridBagConstraints.SOUTHEAST}</li>
 135  * <li>{@code GridBagConstraints.CENTER} (the default)</li>
 136  * </ul>
 137  * <p>Orientation Relative Values:
 138  * <ul>

 139  * <li>{@code GridBagConstraints.PAGE_START}</li>
 140  * <li>{@code GridBagConstraints.PAGE_END}</li>
 141  * <li>{@code GridBagConstraints.LINE_START}</li>
 142  * <li>{@code GridBagConstraints.LINE_END}</li>
 143  * <li>{@code GridBagConstraints.FIRST_LINE_START}</li>
 144  * <li>{@code GridBagConstraints.FIRST_LINE_END}</li>
 145  * <li>{@code GridBagConstraints.LAST_LINE_START}</li>
 146  * <li>{@code GridBagConstraints.LAST_LINE_END}</li>
 147  * </ul>
 148  * <p>Baseline Relative Values:
 149  * <ul>

 150  * <li>{@code GridBagConstraints.BASELINE}</li>
 151  * <li>{@code GridBagConstraints.BASELINE_LEADING}</li>
 152  * <li>{@code GridBagConstraints.BASELINE_TRAILING}</li>
 153  * <li>{@code GridBagConstraints.ABOVE_BASELINE}</li>
 154  * <li>{@code GridBagConstraints.ABOVE_BASELINE_LEADING}</li>
 155  * <li>{@code GridBagConstraints.ABOVE_BASELINE_TRAILING}</li>
 156  * <li>{@code GridBagConstraints.BELOW_BASELINE}</li>
 157  * <li>{@code GridBagConstraints.BELOW_BASELINE_LEADING}</li>
 158  * <li>{@code GridBagConstraints.BELOW_BASELINE_TRAILING}</li>
 159  * </ul>




 160  * <dt>{@link GridBagConstraints#weightx},
 161  * {@link GridBagConstraints#weighty}
 162  * <dd>Used to determine how to distribute space, which is
 163  * important for specifying resizing behavior.
 164  * Unless you specify a weight for at least one component
 165  * in a row ({@code weightx}) and column ({@code weighty}),
 166  * all the components clump together in the center of their container.
 167  * This is because when the weight is zero (the default),
 168  * the {@code GridBagLayout} object puts any extra space
 169  * between its grid of cells and the edges of the container.
 170  * </dl>
 171  * <p>
 172  * Each row may have a baseline; the baseline is determined by the
 173  * components in that row that have a valid baseline and are aligned
 174  * along the baseline (the component's anchor value is one of {@code
 175  * BASELINE}, {@code BASELINE_LEADING} or {@code BASELINE_TRAILING}).
 176  * If none of the components in the row has a valid baseline, the row
 177  * does not have a baseline.
 178  * <p>
 179  * If a component spans rows it is aligned either to the baseline of
 180  * the start row (if the baseline-resize behavior is {@code
 181  * CONSTANT_ASCENT}) or the end row (if the baseline-resize behavior
 182  * is {@code CONSTANT_DESCENT}).  The row that the component is
 183  * aligned to is called the <em>prevailing row</em>.
 184  * <p>
 185  * The following figure shows a baseline layout and includes a
 186  * component that spans rows:
 187  * <div style="float:center;text-align:center;font-weight:bold">
 188  *   <p>Baseline Layout
 189  *   <p><img src="doc-files/GridBagLayout-baseline.png"


 190  *       alt="The following text describes this graphic (Figure 1)." style="float:center">
 191  * </div>

 192  * This layout consists of three components:
 193  * <ul><li>A panel that starts in row 0 and ends in row 1.  The panel
 194  *   has a baseline-resize behavior of {@code CONSTANT_DESCENT} and has
 195  *   an anchor of {@code BASELINE}.  As the baseline-resize behavior
 196  *   is {@code CONSTANT_DESCENT} the prevailing row for the panel is
 197  *   row 1.
 198  * <li>Two buttons, each with a baseline-resize behavior of
 199  *   {@code CENTER_OFFSET} and an anchor of {@code BASELINE}.
 200  * </ul>
 201  * Because the second button and the panel share the same prevailing row,
 202  * they are both aligned along their baseline.
 203  * <p>
 204  * Components positioned using one of the baseline-relative values resize
 205  * differently than when positioned using an absolute or orientation-relative
 206  * value.  How components change is dictated by how the baseline of the
 207  * prevailing row changes.  The baseline is anchored to the
 208  * bottom of the display area if any components with the same prevailing row
 209  * have a baseline-resize behavior of {@code CONSTANT_DESCENT},
 210  * otherwise the baseline is anchored to the top of the display area.
 211  * The following rules dictate the resize behavior:


 220  * <li>Resizable components positioned on the baseline with a
 221  * baseline-resize behavior of {@code OTHER} are only resized if
 222  * the baseline at the resized size fits within the display area.  If
 223  * the baseline is such that it does not fit within the display area
 224  * the component is not resized.
 225  * <li>Components positioned on the baseline that do not have a
 226  * baseline-resize behavior of {@code OTHER}
 227  * can only grow as tall as {@code display height - baseline + baseline of component}.
 228  * </ul>
 229  * If you position a component along the baseline, but the
 230  * component does not have a valid baseline, it will be vertically centered
 231  * in its space.  Similarly if you have positioned a component relative
 232  * to the baseline and none of the components in the row have a valid
 233  * baseline the component is vertically centered.
 234  * <p>
 235  * The following figures show ten components (all buttons)
 236  * managed by a grid bag layout.  Figure 2 shows the layout for a horizontal,
 237  * left-to-right container and Figure 3 shows the layout for a horizontal,
 238  * right-to-left container.
 239  *
 240  * <div style="margin:0 auto;width:680px;text-align:center;font-weight:bold">
 241  *   <div style="float:left">
 242  *     <p><img src="doc-files/GridBagLayout-1.gif"
 243  *        alt="The preceding text describes this graphic (Figure 1)."
 244  *        style="float:center; margin: 7px 10px;">
 245  *     <p>Figure 2: Horizontal, Left-to-Right
 246  *   </div>
 247  *   <div style="float:right">
 248  *     <p><img src="doc-files/GridBagLayout-2.gif"
 249  *        alt="The preceding text describes this graphic (Figure 2)."
 250  *        style="float:center; margin: 7px 10px;">
 251  *     <p>Figure 3: Horizontal, Right-to-Left
 252  *   </div>
 253  *   <br style="clear:both;">
 254  * </div>
 255  * <p>
 256  * Each of the ten components has the {@code fill} field
 257  * of its associated {@code GridBagConstraints} object
 258  * set to {@code GridBagConstraints.BOTH}.
 259  * In addition, the components have the following non-default constraints:
 260  *
 261  * <ul>
 262  * <li>Button1, Button2, Button3: <code>weightx&nbsp;=&nbsp;1.0</code>
 263  * <li>Button4: <code>weightx&nbsp;=&nbsp;1.0</code>,
 264  * <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 265  * <li>Button5: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 266  * <li>Button6: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.RELATIVE</code>
 267  * <li>Button7: <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 268  * <li>Button8: <code>gridheight&nbsp;=&nbsp;2</code>,
 269  * <code>weighty&nbsp;=&nbsp;1.0</code>
 270  * <li>Button9, Button 10:
 271  * <code>gridwidth&nbsp;=&nbsp;GridBagConstraints.REMAINDER</code>
 272  * </ul>
 273  * <p>
 274  * Here is the code that implements the example shown above:


< prev index next >