Example usage for java.awt Component getMinimumSize

List of usage examples for java.awt Component getMinimumSize

Introduction

In this page you can find the example usage for java.awt Component getMinimumSize.

Prototype

public Dimension getMinimumSize() 

Source Link

Document

Gets the minimum size of this component.

Usage

From source file:fxts.stations.ui.SideLayout.java

/**
 * Fills in an instance of <code>SideLayoutInfo</code> for the
 * current set of managed children. This requires three passes through the
 * set of children://from   www .ja  v a 2s  .c om
 * <ol>
 * <li>Figure out the dimensions of the layout grid.
 * <li>Determine which cells the components occupy.
 * <li>Distribute the weights and min sizes amoung the rows/columns.
 * </ol>
 * This also caches the minsizes for all the children when they are
 * first encountered (so subsequent loops don't need to ask again).
 *
 * @param aParent   the layout container
 * @param aSizeflag either <code>PREFERREDSIZE</code> or <code>MINSIZE</code>
 *
 * @return the <code>SideLayoutInfo</code> for the set of children
 */
protected SideLayoutInfo getLayoutInfo(Container aParent, int aSizeflag) {
    synchronized (aParent.getTreeLock()) {
        SideLayoutInfo r = new SideLayoutInfo();
        Component comp;
        SideConstraints constraints;
        Dimension d;
        Component[] components = aParent.getComponents();
        int compindex;
        int i;
        int j;
        int k;
        int px;
        int py;
        int pixels_diff;
        int nextSize;
        int curX, curY, curWidth, curHeight, curRow, curCol;
        double weight_diff;
        double weight;
        double start;
        double size;
        int[] xMax;
        int[] yMax;

        /*
        * Pass #1
        *
        * Figure out the dimensions of the layout grid (use a value of 1 for
        * zero or negative widths and heights).
        */

        r.width = r.height = 0;
        curRow = curCol = -1;
        xMax = new int[MAXGRIDSIZE];
        yMax = new int[MAXGRIDSIZE];
        for (compindex = 0; compindex < components.length; compindex++) {
            comp = components[compindex];
            if (!comp.isVisible()) {
                continue;
            }
            constraints = lookupConstraints(comp);
            curX = constraints.gridx;
            curY = constraints.gridy;
            curWidth = constraints.gridwidth;
            if (curWidth <= 0) {
                curWidth = 1;
            }
            curHeight = constraints.gridheight;
            if (curHeight <= 0) {
                curHeight = 1;
            }

            /* If x or y is negative, then use relative positioning: */
            if (curX < 0 && curY < 0) {
                if (curRow >= 0) {
                    curY = curRow;
                } else if (curCol >= 0) {
                    curX = curCol;
                } else {
                    curY = 0;
                }
            }
            if (curX < 0) {
                px = 0;
                for (i = curY; i < curY + curHeight; i++) {
                    px = Math.max(px, xMax[i]);
                }
                curX = px - curX - 1;
                if (curX < 0) {
                    curX = 0;
                }
            } else if (curY < 0) {
                py = 0;
                for (i = curX; i < curX + curWidth; i++) {
                    py = Math.max(py, yMax[i]);
                }
                curY = py - curY - 1;
                if (curY < 0) {
                    curY = 0;
                }
            }

            /* Adjust the grid width and height */
            for (px = curX + curWidth; r.width < px; r.width++) {
            }
            for (py = curY + curHeight; r.height < py; r.height++) {
            }

            /* Adjust the xMax and yMax arrays */
            for (i = curX; i < curX + curWidth; i++) {
                yMax[i] = py;
            }
            for (i = curY; i < curY + curHeight; i++) {
                xMax[i] = px;
            }

            /* Cache the current slave's size. */
            if (aSizeflag == PREFERREDSIZE) {
                d = comp.getPreferredSize();
            } else {
                d = comp.getMinimumSize();
            }
            constraints.minWidth = d.width;
            //constraints.setMinWidth(d.width);
            constraints.minHeight = d.height;

            /* Zero width and height must mean that this is the last item (or
            * else something is wrong). */
            if (constraints.gridheight == 0 && constraints.gridwidth == 0) {
                curRow = curCol = -1;
            }

            /* Zero width starts a new row */
            if (constraints.gridheight == 0 && curRow < 0) {
                curCol = curX + curWidth;
            }

            /* Zero height starts a new column */
            else if (constraints.gridwidth == 0 && curCol < 0) {
                curRow = curY + curHeight;
            }
        }

        /*
        * Apply minimum row/column dimensions
        */
        if (mColumnWidths != null && r.width < mColumnWidths.length) {
            r.width = mColumnWidths.length;
        }
        if (mRowHeights != null && r.height < mRowHeights.length) {
            r.height = mRowHeights.length;
        }

        /*
        * Pass #2
        *
        * Negative values for gridX are filled in with the current x value.
        * Negative values for gridY are filled in with the current y value.
        * Negative or zero values for gridWidth and gridHeight end the current
        *  row or column, respectively.
        */

        curRow = curCol = -1;
        xMax = new int[MAXGRIDSIZE];
        yMax = new int[MAXGRIDSIZE];
        for (compindex = 0; compindex < components.length; compindex++) {
            comp = components[compindex];
            if (!comp.isVisible()) {
                continue;
            }
            constraints = lookupConstraints(comp);
            curX = constraints.gridx;
            curY = constraints.gridy;
            curWidth = constraints.gridwidth;
            curHeight = constraints.gridheight;

            /* If x or y is negative, then use relative positioning: */
            if (curX < 0 && curY < 0) {
                if (curRow >= 0) {
                    curY = curRow;
                } else if (curCol >= 0) {
                    curX = curCol;
                } else {
                    curY = 0;
                }
            }
            if (curX < 0) {
                if (curHeight <= 0) {
                    curHeight += r.height - curY;
                    if (curHeight < 1) {
                        curHeight = 1;
                    }
                }
                px = 0;
                for (i = curY; i < curY + curHeight; i++) {
                    px = Math.max(px, xMax[i]);
                }
                curX = px - curX - 1;
                if (curX < 0) {
                    curX = 0;
                }
            } else if (curY < 0) {
                if (curWidth <= 0) {
                    curWidth += r.width - curX;
                    if (curWidth < 1) {
                        curWidth = 1;
                    }
                }
                py = 0;
                for (i = curX; i < curX + curWidth; i++) {
                    py = Math.max(py, yMax[i]);
                }
                curY = py - curY - 1;
                if (curY < 0) {
                    curY = 0;
                }
            }
            if (curWidth <= 0) {
                curWidth += r.width - curX;
                if (curWidth < 1) {
                    curWidth = 1;
                }
            }
            if (curHeight <= 0) {
                curHeight += r.height - curY;
                if (curHeight < 1) {
                    curHeight = 1;
                }
            }
            px = curX + curWidth;
            py = curY + curHeight;
            for (i = curX; i < curX + curWidth; i++) {
                yMax[i] = py;
            }
            for (i = curY; i < curY + curHeight; i++) {
                xMax[i] = px;
            }

            /* Make negative sizes start a new row/column */
            if (constraints.gridheight == 0 && constraints.gridwidth == 0) {
                curRow = curCol = -1;
            }
            if (constraints.gridheight == 0 && curRow < 0) {
                curCol = curX + curWidth;
            } else if (constraints.gridwidth == 0 && curCol < 0) {
                curRow = curY + curHeight;
            }

            /* Assign the new values to the gridbag slave */
            constraints.tempX = curX;
            constraints.tempY = curY;
            constraints.tempWidth = curWidth;
            constraints.tempHeight = curHeight;
        }

        /*
        * Apply minimum row/column dimensions and weights
        */
        if (mColumnWidths != null) {
            System.arraycopy(mColumnWidths, 0, r.minWidth, 0, mColumnWidths.length);
        }
        if (mRowHeights != null) {
            System.arraycopy(mRowHeights, 0, r.minHeight, 0, mRowHeights.length);
        }
        if (mColumnWeights != null) {
            System.arraycopy(mColumnWeights, 0, r.weightX, 0, mColumnWeights.length);
        }
        if (mRowWeights != null) {
            System.arraycopy(mRowWeights, 0, r.weightY, 0, mRowWeights.length);
        }

        /*
        * Pass #3
        *
        * Distribute the minimun widths and weights:
        */

        nextSize = Integer.MAX_VALUE;
        for (i = 1; i != Integer.MAX_VALUE; i = nextSize, nextSize = Integer.MAX_VALUE) {
            for (compindex = 0; compindex < components.length; compindex++) {
                comp = components[compindex];
                if (!comp.isVisible()) {
                    continue;
                }
                constraints = lookupConstraints(comp);
                if (constraints.tempWidth == i) {
                    px = constraints.tempX + constraints.tempWidth; /* right column */

                    /*
                    * Figure out if we should use this slave\'s weight.  If the weight
                    * is less than the total weight spanned by the width of the cell,
                    * then discard the weight.  Otherwise split the difference
                    * according to the existing weights.
                    */

                    weight_diff = constraints.weightx;
                    for (k = constraints.tempX; k < px; k++) {
                        weight_diff -= r.weightX[k];
                    }
                    if (weight_diff > 0.0) {
                        weight = 0.0;
                        for (k = constraints.tempX; k < px; k++) {
                            weight += r.weightX[k];
                        }
                        for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
                            double wt = r.weightX[k];
                            double dx = (wt * weight_diff) / weight;
                            r.weightX[k] += dx;
                            weight_diff -= dx;
                            weight -= wt;
                        }
                        /* Assign the remainder to the rightmost cell */
                        r.weightX[px - 1] += weight_diff;
                    }

                    /*
                    * Calculate the minWidth array values.
                    * First, figure out how wide the current slave needs to be.
                    * Then, see if it will fit within the current minWidth values.
                    * If it will not fit, add the difference according to the
                    * weightX array.
                    */

                    pixels_diff = constraints.minWidth + constraints.ipadx + constraints.insets.left
                            + constraints.insets.right;
                    for (k = constraints.tempX; k < px; k++) {
                        pixels_diff -= r.minWidth[k];
                    }
                    if (pixels_diff > 0) {
                        weight = 0.0;
                        for (k = constraints.tempX; k < px; k++) {
                            weight += r.weightX[k];
                        }
                        for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
                            double wt = r.weightX[k];
                            int dx = (int) ((wt * (double) pixels_diff) / weight);
                            r.minWidth[k] += dx;
                            pixels_diff -= dx;
                            weight -= wt;
                        }
                        /* Any leftovers go into the rightmost cell */
                        r.minWidth[px - 1] += pixels_diff;
                    }
                } else if (constraints.tempWidth > i && constraints.tempWidth < nextSize) {
                    nextSize = constraints.tempWidth;
                }
                if (constraints.tempHeight == i) {
                    py = constraints.tempY + constraints.tempHeight; /* bottom row */

                    /*
                    * Figure out if we should use this slave's weight.  If the weight
                    * is less than the total weight spanned by the height of the cell,
                    * then discard the weight.  Otherwise split it the difference
                    * according to the existing weights.
                    */

                    weight_diff = constraints.weighty;
                    for (k = constraints.tempY; k < py; k++) {
                        weight_diff -= r.weightY[k];
                    }
                    if (weight_diff > 0.0) {
                        weight = 0.0;
                        for (k = constraints.tempY; k < py; k++) {
                            weight += r.weightY[k];
                        }
                        for (k = constraints.tempY; weight > 0.0 && k < py; k++) {
                            double wt = r.weightY[k];
                            double dy = (wt * weight_diff) / weight;
                            r.weightY[k] += dy;
                            weight_diff -= dy;
                            weight -= wt;
                        }
                        /* Assign the remainder to the bottom cell */
                        r.weightY[py - 1] += weight_diff;
                    }

                    /*
                    * Calculate the minHeight array values.
                    * First, figure out how tall the current slave needs to be.
                    * Then, see if it will fit within the current minHeight values.
                    * If it will not fit, add the difference according to the
                    * weightY array.
                    */

                    pixels_diff = constraints.minHeight + constraints.ipady + constraints.insets.top
                            + constraints.insets.bottom;
                    for (k = constraints.tempY; k < py; k++) {
                        pixels_diff -= r.minHeight[k];
                    }
                    if (pixels_diff > 0) {
                        weight = 0.0;
                        for (k = constraints.tempY; k < py; k++) {
                            weight += r.weightY[k];
                        }
                        for (k = constraints.tempY; weight > 0.0 && k < py; k++) {
                            double wt = r.weightY[k];
                            int dy = (int) ((wt * (double) pixels_diff) / weight);
                            r.minHeight[k] += dy;
                            pixels_diff -= dy;
                            weight -= wt;
                        }
                        /* Any leftovers go into the bottom cell */
                        r.minHeight[py - 1] += pixels_diff;
                    }
                } else if (constraints.tempHeight > i && constraints.tempHeight < nextSize) {
                    nextSize = constraints.tempHeight;
                }
            }
        }
        return r;
    }
}

From source file:org.kineticsystem.commons.layout.TetrisLayout.java

/**
 * Add the specified component to the layout, using the specified
 * constraint object./*from w w w  .  j a va 2  s.  co  m*/
 * @param comp The component to be added
 * @param constraints Where/how the component is added to the layout.
 */
public void addLayoutComponent(Component comp, Object constraints) {

    if (constraints == null) {
        constraints = new Cell();
    }
    if (constraints instanceof Cell) {

        Cell cell = (Cell) constraints;
        if (cell.getRows() < 0 || cell.getCols() < 0) {
            logger.fatal("Cannot add to layout: " + "cell must have positive row and column number!");
            System.exit(TetrisLayout.FATAL_ERROR);
        }
        setConstraints(comp, cell);

        // Store a copy of all component dimensions.

        Size cd = new Size();
        cd.setMaximumWidth(comp.getMaximumSize().getWidth());
        cd.setMaximumHeight(comp.getMaximumSize().getHeight());
        cd.setMinimumWidth(comp.getMinimumSize().getWidth());
        cd.setMinimumHeight(comp.getMinimumSize().getHeight());
        cd.setPreferredWidth(comp.getPreferredSize().getWidth());
        cd.setPreferredHeight(comp.getPreferredSize().getHeight());

        // Check dimensions.

        if (cd.getMinimumWidth() > cd.getPreferredWidth()) {
            logger.warn("Component minimum width is greater than preferred"
                    + " width: set value to preferred size!\n" + cd);
            cd.setMinimumWidth(cd.getPreferredWidth());
        }
        if (cd.getMaximumWidth() < cd.getPreferredWidth()) {
            logger.warn("Component maximum width is less than preferred"
                    + " width: set value to preferred size!\n" + cd);
            cd.setMaximumWidth(cd.getPreferredWidth());
        }
        if (cd.getMinimumHeight() > cd.getPreferredHeight()) {
            logger.warn("Component minimum height is greater than preferred"
                    + " height: set value to preferred size!\n" + cd);
            cd.setMinimumHeight(cd.getPreferredHeight());
        }
        if (cd.getMaximumHeight() < cd.getPreferredHeight()) {
            logger.warn("Component maximum height is less than preferred"
                    + " height: set value to preferred size!\n" + cd);
            cd.setMaximumHeight(cd.getPreferredHeight());
        }

        componentSizes.put(comp, cd);

    } else if (constraints != null) {
        logger.fatal("Cannot add to layout: constraint must be a Cell!");
        System.exit(TetrisLayout.FATAL_ERROR);
    }
}

From source file:org.nuclos.client.layout.wysiwyg.LayoutMLGenerator.java

/**
 *  Method for converting {@link Dimension} minimumSize to LayoutML XML.
 *
 * @param c/* w w w .j  ava2s  . c  om*/
 * @param blockDeep
 * @return {@link StringBuffer} with the LayoutML
 */
private synchronized StringBuffer getLayoutMLMinimumSizeFromComponent(Component c, int blockDeep) {
    LayoutMLBlock block = new LayoutMLBlock(blockDeep);

    Dimension dim = c.getMinimumSize();
    if (dim != null) {
        // <minimum-size height="30" width="80" />
        block.append("<" + ELEMENT_MINIMUMSIZE + " ");
        block.append(ATTRIBUTE_HEIGHT + "=\"");
        block.append(dim.height);
        block.append("\" " + ATTRIBUTE_WIDTH + "=\"");
        block.append(dim.width);
        block.append("\" />");
    }

    return block.getStringBuffer();
}

From source file:org.pentaho.ui.xul.swing.SwingElement.java

public void layout() {
    super.layout();
    double totalFlex = 0.0;

    if (isVisible() == false) {
        resetContainer();//w  w w.  j  a  v  a 2s .  c  o m
        return;
    }

    for (Element comp : getChildNodes()) {
        // if (comp.getManagedObject() == null) {
        // continue;
        // }
        if (((XulComponent) comp).getFlex() > 0) {
            flexLayout = true;
            totalFlex += ((XulComponent) comp).getFlex();
        }
    }

    double currentFlexTotal = 0.0;

    Align alignment = (getAlign() != null) ? Align.valueOf(this.getAlign().toUpperCase()) : null;

    for (int i = 0; i < getChildNodes().size(); i++) {
        XulComponent comp = (XulComponent) getChildNodes().get(i);
        gc.fill = GridBagConstraints.BOTH;

        if (comp instanceof XulSplitter) {
            JPanel prevContainer = container;
            container = new ScrollablePanel(new GridBagLayout());
            container.setOpaque(false);

            final JSplitPane splitter = new JSplitPane(
                    (this.getOrientation() == Orient.VERTICAL) ? JSplitPane.VERTICAL_SPLIT
                            : JSplitPane.HORIZONTAL_SPLIT,
                    prevContainer, container);
            splitter.setContinuousLayout(true);

            final double splitterSize = currentFlexTotal / totalFlex;
            splitter.setResizeWeight(splitterSize);
            if (totalFlex > 0) {
                splitter.addComponentListener(new ComponentListener() {
                    public void componentHidden(ComponentEvent arg0) {
                    }

                    public void componentMoved(ComponentEvent arg0) {
                    }

                    public void componentShown(ComponentEvent arg0) {
                    }

                    public void componentResized(ComponentEvent arg0) {
                        splitter.setDividerLocation(splitterSize);
                        splitter.removeComponentListener(this);
                    }

                });

            }

            if (!flexLayout) {
                if (this.getOrientation() == Orient.VERTICAL) { // VBox and such
                    gc.weighty = 1.0;
                } else {
                    gc.weightx = 1.0;
                }

                prevContainer.add(Box.createGlue(), gc);
            }
            setManagedObject(splitter);
        }

        Object maybeComponent = comp.getManagedObject();
        if (maybeComponent == null || !(maybeComponent instanceof Component)) {
            continue;
        }
        if (this.getOrientation() == Orient.VERTICAL) { // VBox and such
            gc.gridheight = comp.getFlex() + 1;
            gc.gridwidth = GridBagConstraints.REMAINDER;
            gc.weighty = (totalFlex == 0) ? 0 : (comp.getFlex() / totalFlex);
        } else {
            gc.gridwidth = comp.getFlex() + 1;
            gc.gridheight = GridBagConstraints.REMAINDER;
            gc.weightx = (totalFlex == 0) ? 0 : (comp.getFlex() / totalFlex);
        }

        currentFlexTotal += comp.getFlex();

        if (this.getOrientation() == Orient.VERTICAL) { // VBox and such
            if (alignment != null) {
                gc.fill = GridBagConstraints.NONE;
                switch (alignment) {
                case START:
                    gc.anchor = GridBagConstraints.WEST;
                    break;
                case CENTER:
                    gc.anchor = GridBagConstraints.CENTER;
                    break;
                case END:
                    gc.anchor = GridBagConstraints.EAST;
                    break;
                }
            }

        } else {
            if (alignment != null) {
                gc.fill = GridBagConstraints.NONE;
                switch (alignment) {
                case START:
                    gc.anchor = GridBagConstraints.NORTH;
                    break;
                case CENTER:
                    gc.anchor = GridBagConstraints.CENTER;
                    break;
                case END:
                    gc.anchor = GridBagConstraints.SOUTH;
                    break;
                }
            }
        }

        Component component = (Component) maybeComponent;

        if (comp.getWidth() > 0 || comp.getHeight() > 0) {
            Dimension minSize = component.getMinimumSize();
            Dimension prefSize = component.getPreferredSize();

            if (comp.getWidth() > 0) {
                minSize.width = comp.getWidth();
                prefSize.width = comp.getWidth();
            }
            if (comp.getHeight() > 0) {
                minSize.height = comp.getHeight();
                prefSize.height = comp.getHeight();
            }
            component.setMinimumSize(minSize);
            component.setPreferredSize(prefSize);
        }

        container.add(component, gc);

        if (i + 1 == getChildNodes().size() && !flexLayout) {
            if (this.getOrientation() == Orient.VERTICAL) { // VBox and such
                gc.weighty = 1.0;

            } else {
                gc.weightx = 1.0;

            }
            container.add(Box.createGlue(), gc);
        }
    }

}

From source file:org.pentaho.ui.xul.swing.tags.SwingGrid.java

@Override
public void layout() {

    if (this.getChildNodes().size() < 2) {
        logger.warn("Grid does not contain Column and Row children");
        return;//from   w  ww.  j a va2  s.c  om
    }

    XulComponent columns = this.getChildNodes().get(0);
    XulComponent rows = this.getChildNodes().get(1);

    int colCount = 0;
    int rowCount = 0;
    float colFlexTotal = 0;
    float rowTotalFlex = 0;
    for (XulComponent col : columns.getChildNodes()) {
        if (col.getFlex() > 0) {
            colFlexTotal += col.getFlex();
        }
        colCount++;
    }

    for (XulComponent row : rows.getChildNodes()) {
        if (row.getFlex() > 0) {
            rowTotalFlex += row.getFlex();
        }
        rowCount++;
    }

    for (XulComponent row : rows.getChildNodes()) {
        gc.gridx = 0;

        for (XulComponent xulComp : row.getChildNodes()) {
            gc.weightx = 0.0;
            gc.gridwidth = 1;
            gc.gridheight = 1;
            gc.weighty = 0.0;
            gc.anchor = GridBagConstraints.NORTHWEST;
            gc.fill = GridBagConstraints.NONE;

            Component comp = (Component) xulComp.getManagedObject();
            float colFlex = columns.getChildNodes().get(gc.gridx).getFlex();
            int rowFlex = row.getFlex();

            Align colAlignment = null;
            Align rowAlignment = null;
            String colAlignmentStr = xulComp.getAlign();
            String rowAlignStr = row.getAlign();
            if (colAlignmentStr != null) {
                colAlignment = Align.valueOf(colAlignmentStr);
            }
            if (rowAlignStr != null) {
                rowAlignment = Align.valueOf(rowAlignStr);
            }

            if (colFlex > 0) {
                gc.weightx = (colFlex / colFlexTotal);
            }
            if (rowFlex > 0) {
                gc.weighty = (rowFlex / rowTotalFlex);
            }
            if (colAlignment == Align.STRETCH && xulComp.getFlex() > 0) {
                gc.fill = GridBagConstraints.BOTH;
            } else if (colAlignment == Align.STRETCH) {
                gc.fill = GridBagConstraints.HORIZONTAL;
            } else if (xulComp.getFlex() > 0) {
                gc.fill = GridBagConstraints.VERTICAL;
            }

            if (row.getChildNodes().indexOf(xulComp) + 1 == row.getChildNodes().size()) {
                gc.gridwidth = GridBagConstraints.REMAINDER;
            } else {
                gc.gridwidth = 1;
            }
            if (rows.getChildNodes().indexOf(row) + 1 == rows.getChildNodes().size()) {
                gc.gridheight = GridBagConstraints.REMAINDER;
            } else {
                gc.gridheight = 1;
            }

            // gc.gridheight = row.getFlex() + 1;

            if (colAlignment != null && rowAlignment != null) {
                switch (rowAlignment) {
                case START:
                    switch (colAlignment) {
                    case START:
                        gc.anchor = GridBagConstraints.NORTHWEST;
                        break;
                    case CENTER:
                        gc.anchor = GridBagConstraints.NORTH;
                        break;
                    case END:
                        gc.anchor = GridBagConstraints.NORTHEAST;
                        break;
                    }
                    break;
                case CENTER:
                    switch (colAlignment) {
                    case START:
                        gc.anchor = GridBagConstraints.WEST;
                        break;
                    case CENTER:
                        gc.anchor = GridBagConstraints.CENTER;
                        break;
                    case END:
                        gc.anchor = GridBagConstraints.EAST;
                        break;
                    }
                    break;
                case END:
                    switch (colAlignment) {
                    case START:
                        gc.anchor = GridBagConstraints.SOUTHWEST;
                        break;
                    case CENTER:
                        gc.anchor = GridBagConstraints.SOUTH;
                        break;
                    case END:
                        gc.anchor = GridBagConstraints.SOUTHEAST;
                        break;
                    }
                }
            } else if (rowAlignment != null) {
                switch (rowAlignment) {
                case START:
                    gc.anchor = GridBagConstraints.NORTHWEST;
                    break;
                case CENTER:
                    gc.anchor = GridBagConstraints.WEST;
                    break;
                case END:
                    gc.anchor = GridBagConstraints.SOUTHWEST;
                    break;
                }
            } else if (colAlignment != null) {

                switch (colAlignment) {
                case START:
                    gc.anchor = GridBagConstraints.NORTHWEST;
                    break;
                case CENTER:
                    gc.anchor = GridBagConstraints.NORTH;
                    break;
                case END:
                    gc.anchor = GridBagConstraints.NORTHEAST;
                    break;
                }
            }

            if (comp.getWidth() > 0 || comp.getHeight() > 0) {
                Dimension minSize = comp.getMinimumSize();
                Dimension prefSize = comp.getPreferredSize();

                if (comp.getWidth() > 0) {
                    minSize.width = comp.getWidth();
                    prefSize.width = comp.getWidth();
                }
                if (comp.getHeight() > 0) {
                    minSize.height = comp.getHeight();
                    prefSize.height = comp.getHeight();
                }
                comp.setMinimumSize(minSize);
                comp.setPreferredSize(prefSize);
            } else {
                comp.setPreferredSize(comp.getMinimumSize());
            }

            grid.add(comp, gc);
            gc.gridx++;
        }

        gc.gridy++;
    }

    if (rowTotalFlex == 0) {
        // Add in an extra row at the bottom to push others up
        gc.gridy++;
        gc.weighty = 1;
        gc.fill = gc.REMAINDER;
        grid.add(Box.createGlue(), gc);
    }
    this.initialized = true;
}

From source file:org.processmining.analysis.performance.fsmevaluator.FSMEvaluationMenuUI.java

protected static JPanel packHorizontallyLeftAligned(Component[] components, int leftOffset) {
    JPanel packed = new JPanel();
    packed.setOpaque(false);/*from  w  w w  .  j  a  va  2s .c o m*/
    packed.setLayout(new BoxLayout(packed, BoxLayout.X_AXIS));
    if (leftOffset > 0) {
        packed.add(Box.createHorizontalStrut(leftOffset));
    }
    int minW = 0, minH = 0;
    for (Component comp : components) {
        packed.add(comp);
        Dimension dim = comp.getMinimumSize();
        minW += dim.getWidth();
        minH = Math.max(minH, (int) dim.getHeight());
    }
    packed.add(Box.createHorizontalGlue());
    packed.setMinimumSize(new Dimension(minW, minH));
    packed.setMaximumSize(new Dimension(4000, minH));
    packed.setPreferredSize(new Dimension(4000, minH));
    return packed;
}