< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1995, 2017, 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


 425      * the gridbag or if there are components, they have
 426      * not yet been validated.
 427      *
 428      * @serial
 429      * @see #getLayoutInfo(Container, int)
 430      */
 431     protected GridBagLayoutInfo layoutInfo;
 432 
 433     /**
 434      * This field holds the overrides to the column minimum
 435      * width.  If this field is non-{@code null} the values are
 436      * applied to the gridbag after all of the minimum columns
 437      * widths have been calculated.
 438      * If columnWidths has more elements than the number of
 439      * columns, columns are added to the gridbag to match
 440      * the number of elements in columnWidth.
 441      *
 442      * @serial
 443      * @see #getLayoutDimensions()
 444      */
 445     public int columnWidths[];
 446 
 447     /**
 448      * This field holds the overrides to the row minimum
 449      * heights.  If this field is non-{@code null} the values are
 450      * applied to the gridbag after all of the minimum row
 451      * heights have been calculated.
 452      * If {@code rowHeights} has more elements than the number of
 453      * rows, rows are added to the gridbag to match
 454      * the number of elements in {@code rowHeights}.
 455      *
 456      * @serial
 457      * @see #getLayoutDimensions()
 458      */
 459     public int rowHeights[];
 460 
 461     /**
 462      * This field holds the overrides to the column weights.
 463      * If this field is non-{@code null} the values are
 464      * applied to the gridbag after all of the columns
 465      * weights have been calculated.
 466      * If {@code columnWeights[i] >} weight for column i, then
 467      * column i is assigned the weight in {@code columnWeights[i]}.
 468      * If {@code columnWeights} has more elements than the number
 469      * of columns, the excess elements are ignored - they do
 470      * not cause more columns to be created.
 471      *
 472      * @serial
 473      */
 474     public double columnWeights[];
 475 
 476     /**
 477      * This field holds the overrides to the row weights.
 478      * If this field is non-{@code null} the values are
 479      * applied to the gridbag after all of the rows
 480      * weights have been calculated.
 481      * If {@code rowWeights[i] > } weight for row i, then
 482      * row i is assigned the weight in {@code rowWeights[i]}.
 483      * If {@code rowWeights} has more elements than the number
 484      * of rows, the excess elements are ignored - they do
 485      * not cause more rows to be created.
 486      *
 487      * @serial
 488      */
 489     public double rowWeights[];
 490 
 491     /**
 492      * The component being positioned.  This is set before calling into
 493      * {@code adjustForGravity}.
 494      */
 495     private Component componentAdjusting;
 496 
 497     /**
 498      * Creates a grid bag layout manager.
 499      */
 500     public GridBagLayout () {
 501         comptable = new Hashtable<Component,GridBagConstraints>();
 502         defaultConstraints = new GridBagConstraints();
 503     }
 504 
 505     /**
 506      * Sets the constraints for the specified component in this layout.
 507      * @param       comp the component to be modified
 508      * @param       constraints the constraints to be applied
 509      */


 575         if (layoutInfo != null) {
 576             origin.x = layoutInfo.startx;
 577             origin.y = layoutInfo.starty;
 578         }
 579         return origin;
 580     }
 581 
 582     /**
 583      * Determines column widths and row heights for the layout grid.
 584      * <p>
 585      * Most applications do not call this method directly.
 586      * @return     an array of two arrays, containing the widths
 587      *                       of the layout columns and
 588      *                       the heights of the layout rows
 589      * @since      1.1
 590      */
 591     public int [][] getLayoutDimensions () {
 592         if (layoutInfo == null)
 593             return new int[2][0];
 594 
 595         int dim[][] = new int [2][];
 596         dim[0] = new int[layoutInfo.width];
 597         dim[1] = new int[layoutInfo.height];
 598 
 599         System.arraycopy(layoutInfo.minWidth, 0, dim[0], 0, layoutInfo.width);
 600         System.arraycopy(layoutInfo.minHeight, 0, dim[1], 0, layoutInfo.height);
 601 
 602         return dim;
 603     }
 604 
 605     /**
 606      * Determines the weights of the layout grid's columns and rows.
 607      * Weights are used to calculate how much a given column or row
 608      * stretches beyond its preferred size, if the layout has extra
 609      * room to fill.
 610      * <p>
 611      * Most applications do not call this method directly.
 612      * @return      an array of two arrays, representing the
 613      *                    horizontal weights of the layout columns
 614      *                    and the vertical weights of the layout rows
 615      * @since       1.1
 616      */
 617     public double [][] getLayoutWeights () {
 618         if (layoutInfo == null)
 619             return new double[2][0];
 620 
 621         double weights[][] = new double [2][];
 622         weights[0] = new double[layoutInfo.width];
 623         weights[1] = new double[layoutInfo.height];
 624 
 625         System.arraycopy(layoutInfo.weightX, 0, weights[0], 0, layoutInfo.width);
 626         System.arraycopy(layoutInfo.weightY, 0, weights[1], 0, layoutInfo.height);
 627 
 628         return weights;
 629     }
 630 
 631     /**
 632      * Determines which cell in the layout grid contains the point
 633      * specified by <code>(x,&nbsp;y)</code>. Each cell is identified
 634      * by its column index (ranging from 0 to the number of columns
 635      * minus 1) and its row index (ranging from 0 to the number of
 636      * rows minus 1).
 637      * <p>
 638      * If the <code>(x,&nbsp;y)</code> point lies
 639      * outside the grid, the following rules are used.
 640      * The column index is returned as zero if {@code x} lies to the
 641      * left of the layout for a left-to-right container or to the right of


 912      * This method should only be used internally by
 913      * {@code GridBagLayout}.
 914      *
 915      * @param parent  the layout container
 916      * @param sizeflag either {@code PREFERREDSIZE} or
 917      *   {@code MINSIZE}
 918      * @return the {@code GridBagLayoutInfo} for the set of children
 919      * @since 1.4
 920      */
 921     protected GridBagLayoutInfo getLayoutInfo(Container parent, int sizeflag) {
 922         return GetLayoutInfo(parent, sizeflag);
 923     }
 924 
 925     /*
 926      * Calculate maximum array sizes to allocate arrays without ensureCapacity
 927      * we may use preCalculated sizes in whole class because of upper estimation of
 928      * maximumArrayXIndex and maximumArrayYIndex.
 929      */
 930 
 931     private long[]  preInitMaximumArraySizes(Container parent){
 932         Component components[] = parent.getComponents();
 933         Component comp;
 934         GridBagConstraints constraints;
 935         int curX, curY;
 936         int curWidth, curHeight;
 937         int preMaximumArrayXIndex = 0;
 938         int preMaximumArrayYIndex = 0;
 939         long [] returnArray = new long[2];
 940 
 941         for (int compId = 0 ; compId < components.length ; compId++) {
 942             comp = components[compId];
 943             if (!comp.isVisible()) {
 944                 continue;
 945             }
 946 
 947             constraints = lookupConstraints(comp);
 948             curX = constraints.gridx;
 949             curY = constraints.gridy;
 950             curWidth = constraints.gridwidth;
 951             curHeight = constraints.gridheight;
 952 


 986     /**
 987      * This method is obsolete and supplied for backwards
 988      * compatibility only; new code should call {@link
 989      * #getLayoutInfo(java.awt.Container, int) getLayoutInfo} instead.
 990      *
 991      * Fills in an instance of {@code GridBagLayoutInfo} for the
 992      * current set of managed children. This method is the same
 993      * as {@code getLayoutInfo}; refer to {@code getLayoutInfo}
 994      * description for details.
 995      *
 996      * @param  parent the layout container
 997      * @param  sizeflag either {@code PREFERREDSIZE} or {@code MINSIZE}
 998      * @return the {@code GridBagLayoutInfo} for the set of children
 999      */
1000     protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) {
1001         synchronized (parent.getTreeLock()) {
1002             GridBagLayoutInfo r;
1003             Component comp;
1004             GridBagConstraints constraints;
1005             Dimension d;
1006             Component components[] = parent.getComponents();
1007             // Code below will address index curX+curWidth in the case of yMaxArray, weightY
1008             // ( respectively curY+curHeight for xMaxArray, weightX ) where
1009             //  curX in 0 to preInitMaximumArraySizes.y
1010             // Thus, the maximum index that could
1011             // be calculated in the following code is curX+curX.
1012             // EmpericMultier equals 2 because of this.
1013 
1014             int layoutWidth, layoutHeight;
1015             int []xMaxArray;
1016             int []yMaxArray;
1017             int compindex, i, k, px, py, pixels_diff, nextSize;
1018             int curX = 0; // constraints.gridx
1019             int curY = 0; // constraints.gridy
1020             int curWidth = 1;  // constraints.gridwidth
1021             int curHeight = 1;  // constraints.gridheight
1022             int curRow, curCol;
1023             double weight_diff, weight;
1024             int maximumArrayXIndex = 0;
1025             int maximumArrayYIndex = 0;
1026             int anchor;


2046      * @param parent the layout container
2047      * @since 1.4
2048      */
2049     protected void arrangeGrid(Container parent) {
2050         ArrangeGrid(parent);
2051     }
2052 
2053     /**
2054      * This method is obsolete and supplied for backwards
2055      * compatibility only; new code should call {@link
2056      * #arrangeGrid(Container) arrangeGrid} instead.
2057      * This method is the same as {@code arrangeGrid}
2058      *
2059      * @param  parent the layout container
2060      */
2061     protected void ArrangeGrid(Container parent) {
2062         Component comp;
2063         int compindex;
2064         GridBagConstraints constraints;
2065         Insets insets = parent.getInsets();
2066         Component components[] = parent.getComponents();
2067         Dimension d;
2068         Rectangle r = new Rectangle();
2069         int i, diffw, diffh;
2070         double weight;
2071         GridBagLayoutInfo info;
2072 
2073         rightToLeft = !parent.getComponentOrientation().isLeftToRight();
2074 
2075         /*
2076          * If the parent has no slaves anymore, then don't do anything
2077          * at all:  just leave the parent's size as-is.
2078          */
2079         if (components.length == 0 &&
2080             (columnWidths == null || columnWidths.length == 0) &&
2081             (rowHeights == null || rowHeights.length == 0)) {
2082             return;
2083         }
2084 
2085         /*
2086          * Pass #1: scan all the slaves to figure out the total amount


   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


 425      * the gridbag or if there are components, they have
 426      * not yet been validated.
 427      *
 428      * @serial
 429      * @see #getLayoutInfo(Container, int)
 430      */
 431     protected GridBagLayoutInfo layoutInfo;
 432 
 433     /**
 434      * This field holds the overrides to the column minimum
 435      * width.  If this field is non-{@code null} the values are
 436      * applied to the gridbag after all of the minimum columns
 437      * widths have been calculated.
 438      * If columnWidths has more elements than the number of
 439      * columns, columns are added to the gridbag to match
 440      * the number of elements in columnWidth.
 441      *
 442      * @serial
 443      * @see #getLayoutDimensions()
 444      */
 445     public int[] columnWidths;
 446 
 447     /**
 448      * This field holds the overrides to the row minimum
 449      * heights.  If this field is non-{@code null} the values are
 450      * applied to the gridbag after all of the minimum row
 451      * heights have been calculated.
 452      * If {@code rowHeights} has more elements than the number of
 453      * rows, rows are added to the gridbag to match
 454      * the number of elements in {@code rowHeights}.
 455      *
 456      * @serial
 457      * @see #getLayoutDimensions()
 458      */
 459     public int[] rowHeights;
 460 
 461     /**
 462      * This field holds the overrides to the column weights.
 463      * If this field is non-{@code null} the values are
 464      * applied to the gridbag after all of the columns
 465      * weights have been calculated.
 466      * If {@code columnWeights[i] >} weight for column i, then
 467      * column i is assigned the weight in {@code columnWeights[i]}.
 468      * If {@code columnWeights} has more elements than the number
 469      * of columns, the excess elements are ignored - they do
 470      * not cause more columns to be created.
 471      *
 472      * @serial
 473      */
 474     public double[] columnWeights;
 475 
 476     /**
 477      * This field holds the overrides to the row weights.
 478      * If this field is non-{@code null} the values are
 479      * applied to the gridbag after all of the rows
 480      * weights have been calculated.
 481      * If {@code rowWeights[i] > } weight for row i, then
 482      * row i is assigned the weight in {@code rowWeights[i]}.
 483      * If {@code rowWeights} has more elements than the number
 484      * of rows, the excess elements are ignored - they do
 485      * not cause more rows to be created.
 486      *
 487      * @serial
 488      */
 489     public double[] rowWeights;
 490 
 491     /**
 492      * The component being positioned.  This is set before calling into
 493      * {@code adjustForGravity}.
 494      */
 495     private Component componentAdjusting;
 496 
 497     /**
 498      * Creates a grid bag layout manager.
 499      */
 500     public GridBagLayout () {
 501         comptable = new Hashtable<Component,GridBagConstraints>();
 502         defaultConstraints = new GridBagConstraints();
 503     }
 504 
 505     /**
 506      * Sets the constraints for the specified component in this layout.
 507      * @param       comp the component to be modified
 508      * @param       constraints the constraints to be applied
 509      */


 575         if (layoutInfo != null) {
 576             origin.x = layoutInfo.startx;
 577             origin.y = layoutInfo.starty;
 578         }
 579         return origin;
 580     }
 581 
 582     /**
 583      * Determines column widths and row heights for the layout grid.
 584      * <p>
 585      * Most applications do not call this method directly.
 586      * @return     an array of two arrays, containing the widths
 587      *                       of the layout columns and
 588      *                       the heights of the layout rows
 589      * @since      1.1
 590      */
 591     public int [][] getLayoutDimensions () {
 592         if (layoutInfo == null)
 593             return new int[2][0];
 594 
 595         int[][] dim = new int [2][];
 596         dim[0] = new int[layoutInfo.width];
 597         dim[1] = new int[layoutInfo.height];
 598 
 599         System.arraycopy(layoutInfo.minWidth, 0, dim[0], 0, layoutInfo.width);
 600         System.arraycopy(layoutInfo.minHeight, 0, dim[1], 0, layoutInfo.height);
 601 
 602         return dim;
 603     }
 604 
 605     /**
 606      * Determines the weights of the layout grid's columns and rows.
 607      * Weights are used to calculate how much a given column or row
 608      * stretches beyond its preferred size, if the layout has extra
 609      * room to fill.
 610      * <p>
 611      * Most applications do not call this method directly.
 612      * @return      an array of two arrays, representing the
 613      *                    horizontal weights of the layout columns
 614      *                    and the vertical weights of the layout rows
 615      * @since       1.1
 616      */
 617     public double [][] getLayoutWeights () {
 618         if (layoutInfo == null)
 619             return new double[2][0];
 620 
 621         double[][] weights = new double [2][];
 622         weights[0] = new double[layoutInfo.width];
 623         weights[1] = new double[layoutInfo.height];
 624 
 625         System.arraycopy(layoutInfo.weightX, 0, weights[0], 0, layoutInfo.width);
 626         System.arraycopy(layoutInfo.weightY, 0, weights[1], 0, layoutInfo.height);
 627 
 628         return weights;
 629     }
 630 
 631     /**
 632      * Determines which cell in the layout grid contains the point
 633      * specified by <code>(x,&nbsp;y)</code>. Each cell is identified
 634      * by its column index (ranging from 0 to the number of columns
 635      * minus 1) and its row index (ranging from 0 to the number of
 636      * rows minus 1).
 637      * <p>
 638      * If the <code>(x,&nbsp;y)</code> point lies
 639      * outside the grid, the following rules are used.
 640      * The column index is returned as zero if {@code x} lies to the
 641      * left of the layout for a left-to-right container or to the right of


 912      * This method should only be used internally by
 913      * {@code GridBagLayout}.
 914      *
 915      * @param parent  the layout container
 916      * @param sizeflag either {@code PREFERREDSIZE} or
 917      *   {@code MINSIZE}
 918      * @return the {@code GridBagLayoutInfo} for the set of children
 919      * @since 1.4
 920      */
 921     protected GridBagLayoutInfo getLayoutInfo(Container parent, int sizeflag) {
 922         return GetLayoutInfo(parent, sizeflag);
 923     }
 924 
 925     /*
 926      * Calculate maximum array sizes to allocate arrays without ensureCapacity
 927      * we may use preCalculated sizes in whole class because of upper estimation of
 928      * maximumArrayXIndex and maximumArrayYIndex.
 929      */
 930 
 931     private long[]  preInitMaximumArraySizes(Container parent){
 932         Component[] components = parent.getComponents();
 933         Component comp;
 934         GridBagConstraints constraints;
 935         int curX, curY;
 936         int curWidth, curHeight;
 937         int preMaximumArrayXIndex = 0;
 938         int preMaximumArrayYIndex = 0;
 939         long [] returnArray = new long[2];
 940 
 941         for (int compId = 0 ; compId < components.length ; compId++) {
 942             comp = components[compId];
 943             if (!comp.isVisible()) {
 944                 continue;
 945             }
 946 
 947             constraints = lookupConstraints(comp);
 948             curX = constraints.gridx;
 949             curY = constraints.gridy;
 950             curWidth = constraints.gridwidth;
 951             curHeight = constraints.gridheight;
 952 


 986     /**
 987      * This method is obsolete and supplied for backwards
 988      * compatibility only; new code should call {@link
 989      * #getLayoutInfo(java.awt.Container, int) getLayoutInfo} instead.
 990      *
 991      * Fills in an instance of {@code GridBagLayoutInfo} for the
 992      * current set of managed children. This method is the same
 993      * as {@code getLayoutInfo}; refer to {@code getLayoutInfo}
 994      * description for details.
 995      *
 996      * @param  parent the layout container
 997      * @param  sizeflag either {@code PREFERREDSIZE} or {@code MINSIZE}
 998      * @return the {@code GridBagLayoutInfo} for the set of children
 999      */
1000     protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) {
1001         synchronized (parent.getTreeLock()) {
1002             GridBagLayoutInfo r;
1003             Component comp;
1004             GridBagConstraints constraints;
1005             Dimension d;
1006             Component[] components = parent.getComponents();
1007             // Code below will address index curX+curWidth in the case of yMaxArray, weightY
1008             // ( respectively curY+curHeight for xMaxArray, weightX ) where
1009             //  curX in 0 to preInitMaximumArraySizes.y
1010             // Thus, the maximum index that could
1011             // be calculated in the following code is curX+curX.
1012             // EmpericMultier equals 2 because of this.
1013 
1014             int layoutWidth, layoutHeight;
1015             int []xMaxArray;
1016             int []yMaxArray;
1017             int compindex, i, k, px, py, pixels_diff, nextSize;
1018             int curX = 0; // constraints.gridx
1019             int curY = 0; // constraints.gridy
1020             int curWidth = 1;  // constraints.gridwidth
1021             int curHeight = 1;  // constraints.gridheight
1022             int curRow, curCol;
1023             double weight_diff, weight;
1024             int maximumArrayXIndex = 0;
1025             int maximumArrayYIndex = 0;
1026             int anchor;


2046      * @param parent the layout container
2047      * @since 1.4
2048      */
2049     protected void arrangeGrid(Container parent) {
2050         ArrangeGrid(parent);
2051     }
2052 
2053     /**
2054      * This method is obsolete and supplied for backwards
2055      * compatibility only; new code should call {@link
2056      * #arrangeGrid(Container) arrangeGrid} instead.
2057      * This method is the same as {@code arrangeGrid}
2058      *
2059      * @param  parent the layout container
2060      */
2061     protected void ArrangeGrid(Container parent) {
2062         Component comp;
2063         int compindex;
2064         GridBagConstraints constraints;
2065         Insets insets = parent.getInsets();
2066         Component[] components = parent.getComponents();
2067         Dimension d;
2068         Rectangle r = new Rectangle();
2069         int i, diffw, diffh;
2070         double weight;
2071         GridBagLayoutInfo info;
2072 
2073         rightToLeft = !parent.getComponentOrientation().isLeftToRight();
2074 
2075         /*
2076          * If the parent has no slaves anymore, then don't do anything
2077          * at all:  just leave the parent's size as-is.
2078          */
2079         if (components.length == 0 &&
2080             (columnWidths == null || columnWidths.length == 0) &&
2081             (rowHeights == null || rowHeights.length == 0)) {
2082             return;
2083         }
2084 
2085         /*
2086          * Pass #1: scan all the slaves to figure out the total amount


< prev index next >