< prev index next >

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

Print this page
rev 56131 : 8225372: accessibility errors in tables in java.desktop files
Reviewed-by: aivanov
   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  * <ul>
 126  *   <li>Absolute Values:
 127  *     <ul>
 128  *       <li>{@code GridBagConstraints.NORTH}
 129  *       <li>{@code GridBagConstraints.SOUTH}
 130  *       <li>{@code GridBagConstraints.WEST}
 131  *       <li>{@code GridBagConstraints.EAST}
 132  *       <li>{@code GridBagConstraints.NORTHWEST}
 133  *       <li>{@code GridBagConstraints.NORTHEAST}
 134  *       <li>{@code GridBagConstraints.SOUTHWEST}
 135  *       <li>{@code GridBagConstraints.SOUTHEAST}
 136  *       <li>{@code GridBagConstraints.CENTER} (the default)










 137  *     </ul>
 138  *   <li>Orientation Relative Values:
 139  *     <ul >
 140  *       <li>{@code GridBagConstraints.PAGE_START}
 141  *       <li>{@code GridBagConstraints.PAGE_END}
 142  *       <li>{@code GridBagConstraints.LINE_START}
 143  *       <li>{@code GridBagConstraints.LINE_END}
 144  *       <li>{@code GridBagConstraints.FIRST_LINE_START}
 145  *       <li>{@code GridBagConstraints.FIRST_LINE_END}
 146  *       <li>{@code GridBagConstraints.LAST_LINE_START}
 147  *       <li>{@code GridBagConstraints.LAST_LINE_END}
 148  *     </ul>
 149  *   <li>Baseline Relative Values:
 150  *     <ul>
 151  *       <li>{@code GridBagConstraints.BASELINE}
 152  *       <li>{@code GridBagConstraints.BASELINE_LEADING}
 153  *       <li>{@code GridBagConstraints.BASELINE_TRAILING}
 154  *       <li>{@code GridBagConstraints.ABOVE_BASELINE}
 155  *       <li>{@code GridBagConstraints.ABOVE_BASELINE_LEADING}
 156  *       <li>{@code GridBagConstraints.ABOVE_BASELINE_TRAILING}
 157  *       <li>{@code GridBagConstraints.BELOW_BASELINE}
 158  *       <li>{@code GridBagConstraints.BELOW_BASELINE_LEADING}
 159  *       <li>{@code GridBagConstraints.BELOW_BASELINE_TRAILING}
 160  *     </ul>












 161  * </ul>




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



 190  *   <img src="doc-files/GridBagLayout-baseline.png"
 191  *   alt="The following text describes this graphic (Figure 1).">
 192  * </p>

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


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


< prev index next >