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:EdgeLayoutExample.java

public Dimension minimumLayoutSize(Container parent) {
    synchronized (parent.getTreeLock()) {
        int width = 0;
        int height = 0;

        //Add the minimum  widths of all EAST/WEST components
        //Add the minimum height of all NORTH/SOUTH components
        for (int i = 0; i < this.components.size(); i++) {
            Component c = (Component) this.components.get(i);
            if (this.constraints.get(c).equals(WEST) || this.constraints.get(c).equals(EAST)) {
                width += c.getMinimumSize().getWidth();
            } else {
                height += c.getMinimumSize().getHeight();
            }/*  ww  w .  j  a v a2  s  . c om*/
        }

        width += parent.getInsets().right + parent.getInsets().left;
        height += parent.getInsets().top + parent.getInsets().bottom;

        return new Dimension(width, height);
    }
}

From source file:ColumnLayout.java

protected Dimension layoutSize(Container parent, int sizetype) {
    int nkids = parent.getComponentCount();
    Dimension size = new Dimension(0, 0);
    Insets insets = parent.getInsets();
    int num_visible_kids = 0;

    // Compute maximum width and total height of all visible kids
    for (int i = 0; i < nkids; i++) {
        Component kid = parent.getComponent(i);
        Dimension d;/*from  w  w w  . j a  va 2s  .c  o m*/
        if (!kid.isVisible())
            continue;
        num_visible_kids++;
        if (sizetype == 1)
            d = kid.getPreferredSize();
        else if (sizetype == 2)
            d = kid.getMinimumSize();
        else
            d = kid.getMaximumSize();
        if (d.width > size.width)
            size.width = d.width;
        size.height += d.height;
    }

    // Now add in margins and stuff
    size.width += insets.left + insets.right + 2 * margin_width;
    size.height += insets.top + insets.bottom + 2 * margin_height;
    if (num_visible_kids > 1)
        size.height += (num_visible_kids - 1) * spacing;
    return size;
}

From source file:Main.java

private void setSizes(Container parent) {
    int nComps = parent.getComponentCount();
    Dimension d = null;/*from   w  w  w  . ja v a  2  s. c  o m*/

    //Reset preferred/minimum width and height.
    preferredWidth = 0;
    preferredHeight = 0;
    minWidth = 0;
    minHeight = 0;

    for (int i = 0; i < nComps; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            d = c.getPreferredSize();

            if (i > 0) {
                preferredWidth += d.width / 2;
                preferredHeight += vgap;
            } else {
                preferredWidth = d.width;
            }
            preferredHeight += d.height;

            minWidth = Math.max(c.getMinimumSize().width, minWidth);
            minHeight = preferredHeight;
        }
    }
}

From source file:XYLayout.java

Rectangle getComponentBounds(Component component, boolean doPreferred) {
    XYConstraints constraints = (XYConstraints) info.get(component);
    if (constraints == null)
        constraints = defaultConstraints;
    Rectangle r = new Rectangle(constraints.x, constraints.y, constraints.width, constraints.height);
    if (r.width <= 0 || r.height <= 0) {
        Dimension d = doPreferred ? component.getPreferredSize() : component.getMinimumSize();
        if (r.width <= 0)
            r.width = d.width;/*w w  w  .  j a v a  2s  .c o m*/
        if (r.height <= 0)
            r.height = d.height;
    }
    return r;
}

From source file:GraphPaperLayout.java

/**
 * Algorithm for calculating the largest minimum or preferred cell size.
 * <P>/*from  w ww .  j a v a2 s  .  c o  m*/
 * Largest cell size is calculated by getting the applicable size of each
 * component and keeping the maximum value, dividing the component's width by
 * the number of columns it is specified to occupy and dividing the
 * component's height by the number of rows it is specified to occupy.
 * 
 * @param parent
 *          the container in which to do the layout.
 * @param isPreferred
 *          true for calculating preferred size, false for calculating minimum
 *          size.
 * @return the largest cell size required.
 */
protected Dimension getLargestCellSize(Container parent, boolean isPreferred) {
    int ncomponents = parent.getComponentCount();
    Dimension maxCellSize = new Dimension(0, 0);
    for (int i = 0; i < ncomponents; i++) {
        Component c = parent.getComponent(i);
        Rectangle rect = compTable.get(c);
        if (c != null && rect != null) {
            Dimension componentSize;
            if (isPreferred) {
                componentSize = c.getPreferredSize();
            } else {
                componentSize = c.getMinimumSize();
            }
            // Note: rect dimensions are already asserted to be > 0 when the
            // component is added with constraints
            maxCellSize.width = Math.max(maxCellSize.width, componentSize.width / rect.width);
            maxCellSize.height = Math.max(maxCellSize.height, componentSize.height / rect.height);
        }
    }
    return maxCellSize;
}

From source file:GraphPaperTest.java

/**
 * Algorithm for calculating the largest minimum or preferred cell size.
 * <p>//from  w  w w  .j a  v  a2 s  . co m
 * Largest cell size is calculated by getting the applicable size of each
 * component and keeping the maximum value, dividing the component's width
 * by the number of columns it is specified to occupy and dividing the
 * component's height by the number of rows it is specified to occupy.
 * 
 * @param parent
 *            the container in which to do the layout.
 * @param isPreferred
 *            true for calculating preferred size, false for calculating
 *            minimum size.
 * @return the largest cell size required.
 */
protected Dimension getLargestCellSize(Container parent, boolean isPreferred) {
    int ncomponents = parent.getComponentCount();
    Dimension maxCellSize = new Dimension(0, 0);
    for (int i = 0; i < ncomponents; i++) {
        Component c = parent.getComponent(i);
        Rectangle rect = (Rectangle) compTable.get(c);
        if (c != null && rect != null) {
            Dimension componentSize;
            if (isPreferred) {
                componentSize = c.getPreferredSize();
            } else {
                componentSize = c.getMinimumSize();
            }
            // Note: rect dimensions are already asserted to be > 0 when the
            // component is added with constraints
            maxCellSize.width = Math.max(maxCellSize.width, componentSize.width / rect.width);
            maxCellSize.height = Math.max(maxCellSize.height, componentSize.height / rect.height);
        }
    }
    return maxCellSize;
}

From source file:TableLayout.java

private void measureComponents(Container parent) {
    // set basic metrics such as ncomponents & nrows, and load the components
    // into the components[][] array.
    loadComponents(parent);//from   ww  w.  j  av a  2 s.  c  o m

    // allocate new arrays to store row and column preferred and min sizes, but
    // only if the old arrays aren't big enough
    if (minWidth == null || minWidth.length < ncols) {
        minWidth = new int[ncols];
        prefWidth = new int[ncols];
        columnWidth = new int[ncols];
        weight = new int[ncols];
    }
    if (minHeight == null || minHeight.length < nrows) {
        minHeight = new int[nrows];
        prefHeight = new int[nrows];
    }

    int i;
    for (i = 0; i < ncols; ++i) {
        minWidth[i] = 0;
        prefWidth[i] = 0;
    }
    for (i = 0; i < nrows; ++i) {
        minHeight[i] = 0;
        prefHeight[i] = 0;
    }

    // measure the minimum and preferred size of each row and column

    for (int row = 0; row < nrows; ++row) {
        for (int col = 0; col < ncols; ++col) {
            Component comp = components[col][row];
            if (comp != null) {
                TableOption option = (TableOption) options.get(comp);
                if (option == null)
                    option = defaultOption;

                Dimension minSize = new Dimension(comp.getMinimumSize());
                Dimension prefSize = new Dimension(comp.getPreferredSize());

                // enforce prefSize>=minSize
                if (prefSize.width < minSize.width)
                    prefSize.width = minSize.width;
                if (prefSize.height < minSize.height)
                    prefSize.height = minSize.height;

                // divide size across all the rows or columns being spanned
                minSize.width /= option.colSpan;
                minSize.height /= option.rowSpan;
                prefSize.width = (prefSize.width - hgap * (option.colSpan - 1)) / option.colSpan;
                prefSize.height = (prefSize.height - vgap * (option.rowSpan - 1)) / option.rowSpan;

                for (int c = 0; c < option.colSpan; ++c) {
                    if (minSize.width > minWidth[col + c])
                        minWidth[col + c] = minSize.width;
                    if (prefSize.width > prefWidth[col + c])
                        prefWidth[col + c] = prefSize.width;
                }

                for (int r = 0; r < option.rowSpan; ++r) {
                    if (minSize.height > minHeight[row + r])
                        minHeight[row + r] = minSize.height;
                    if (prefSize.height > prefHeight[row + r])
                        prefHeight[row + r] = prefSize.height;
                }
            }
        }
    }

    // add rows and columns to give total min and preferred size of whole grid

    MinWidth = 0;
    MinHeight = 0;
    PrefWidth = hgap;
    PrefHeight = vgap;

    for (i = 0; i < ncols; ++i) {
        MinWidth += minWidth[i];
        PrefWidth += prefWidth[i] + hgap;
    }
    for (i = 0; i < nrows; ++i) {
        MinHeight += minHeight[i];
        PrefHeight += prefHeight[i] + vgap;
    }
}

From source file:EdgeLayoutExample.java

public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        Insets insets = parent.getInsets();
        int top = insets.top;
        int left = insets.left;

        Dimension minimumSize = minimumLayoutSize(parent);

        int height = minimumSize.height;
        int width = minimumSize.width;

        int availableHeight = parent.getHeight() - insets.bottom - insets.top;
        int availableWidth = parent.getWidth() - insets.left - insets.right;
        if (height < availableHeight) {
            height = availableHeight;//from  w ww . j a va2s.  c  om
        }
        if (width < availableWidth) {
            width = availableWidth;
        }

        int bottom = availableHeight;
        int right = availableWidth;

        Dimension preferredSize = preferredLayoutSize(parent);

        int preferredWidthAvailable = width - preferredSize.width;
        int preferredHeightAvailable = height - preferredSize.height;

        Component centerComp = null;

        for (int i = 0; i < this.components.size(); i++) {
            Component c = (Component) this.components.get(i);
            String constraint = (String) this.constraints.get(c);

            if (constraint.equals(CENTER)) {
                centerComp = c;
            } else {
                int compHeight;
                int compWidth;
                int xOrigin;
                int yOrigin;

                if (constraint.equals(NORTH) || constraint.equals(SOUTH)) {
                    compWidth = width;

                    if (preferredHeightAvailable > 0) {
                        int preferredHeightNeeded = c.getPreferredSize().height - c.getMinimumSize().height;
                        if (preferredHeightAvailable > preferredHeightNeeded) {
                            compHeight = c.getPreferredSize().height;
                            preferredHeightAvailable -= preferredHeightNeeded;
                        } else {
                            compHeight = c.getMinimumSize().height + preferredHeightAvailable;
                            preferredHeightAvailable = 0;
                        }
                    } else {
                        compHeight = c.getMinimumSize().height;
                    }
                    height = height - compHeight;

                    xOrigin = left;

                    if (constraint.equals(NORTH)) {
                        yOrigin = top;
                        top += compHeight;
                    } else {
                        yOrigin = bottom - compHeight;
                        bottom = yOrigin;
                    }
                } else {
                    compHeight = height;
                    if (preferredWidthAvailable > 0) {
                        int preferredWidthNeeded = c.getPreferredSize().width - c.getMinimumSize().width;
                        if (preferredWidthAvailable > preferredWidthNeeded) {
                            compWidth = c.getPreferredSize().width;
                            preferredWidthAvailable -= preferredWidthNeeded;
                        } else {
                            compWidth = c.getMinimumSize().width + preferredWidthAvailable;
                            preferredWidthAvailable = 0;
                        }
                    } else {
                        compWidth = c.getMinimumSize().width;
                    }
                    width = width - compWidth;

                    yOrigin = top;

                    if (constraint.equals(WEST)) {
                        xOrigin = left;
                        left += compWidth;
                    } else {
                        xOrigin = right - compWidth;
                        right = xOrigin;
                    }
                }
                c.setSize(compWidth, compHeight);
                c.setBounds(xOrigin, yOrigin, compWidth, compHeight);
            }
            if (centerComp != null) {
                c.setSize(width, height);
                c.setBounds(left, top, width, height);
            }
        }
    }
}

From source file:FormatLayout.java

/**
 * Returns the minimum size of the component using this layout manager.
 *
 * @param parent  the parent.//from  w w  w  . j  a  va2  s . c  o m
 *
 * @return the minimum size of the component
 */
public Dimension minimumLayoutSize(final Container parent) {

    Component c0, c1, c2, c3, c4, c5;

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        int componentIndex = 0;
        final int rowCount = this.rowHeights.length;
        for (int i = 0; i < this.columnWidths.length; i++) {
            this.columnWidths[i] = 0;
        }
        this.columns1and2Width = 0;
        this.columns4and5Width = 0;
        this.columns1to4Width = 0;
        this.columns1to5Width = 0;
        this.columns0to5Width = 0;
        final int totalHeight = 0;
        for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {

            final int format = this.rowFormats[rowIndex % this.rowFormats.length];

            switch (format) {
            case FormatLayout.C:
                c0 = parent.getComponent(componentIndex);
                this.columns0to5Width = Math.max(this.columns0to5Width, c0.getMinimumSize().width);
                componentIndex = componentIndex + 1;
                break;
            case FormatLayout.LC:
                c0 = parent.getComponent(componentIndex);
                c1 = parent.getComponent(componentIndex + 1);
                updateLC(rowIndex, c0.getMinimumSize(), c1.getMinimumSize());
                componentIndex = componentIndex + 2;
                break;
            case FormatLayout.LCB:
                c0 = parent.getComponent(componentIndex);
                c1 = parent.getComponent(componentIndex + 1);
                c2 = parent.getComponent(componentIndex + 2);
                updateLCB(rowIndex, c0.getMinimumSize(), c1.getMinimumSize(), c2.getMinimumSize());
                componentIndex = componentIndex + 3;
                break;
            case FormatLayout.LCLC:
                c0 = parent.getComponent(componentIndex);
                c1 = parent.getComponent(componentIndex + 1);
                c2 = parent.getComponent(componentIndex + 2);
                c3 = parent.getComponent(componentIndex + 3);
                updateLCLC(rowIndex, c0.getMinimumSize(), c1.getMinimumSize(), c2.getMinimumSize(),
                        c3.getMinimumSize());
                componentIndex = componentIndex + 3;
                break;
            case FormatLayout.LCBLC:
                c0 = parent.getComponent(componentIndex);
                c1 = parent.getComponent(componentIndex + 1);
                c2 = parent.getComponent(componentIndex + 2);
                c3 = parent.getComponent(componentIndex + 3);
                c4 = parent.getComponent(componentIndex + 4);
                updateLCBLC(rowIndex, c0.getMinimumSize(), c1.getMinimumSize(), c2.getMinimumSize(),
                        c3.getMinimumSize(), c4.getMinimumSize());
                componentIndex = componentIndex + 4;
                break;
            case FormatLayout.LCLCB:
                c0 = parent.getComponent(componentIndex);
                c1 = parent.getComponent(componentIndex + 1);
                c2 = parent.getComponent(componentIndex + 2);
                c3 = parent.getComponent(componentIndex + 3);
                c4 = parent.getComponent(componentIndex + 4);
                updateLCLCB(rowIndex, c0.getMinimumSize(), c1.getMinimumSize(), c2.getMinimumSize(),
                        c3.getMinimumSize(), c4.getMinimumSize());
                componentIndex = componentIndex + 4;
                break;
            case FormatLayout.LCBLCB:
                c0 = parent.getComponent(componentIndex);
                c1 = parent.getComponent(componentIndex + 1);
                c2 = parent.getComponent(componentIndex + 2);
                c3 = parent.getComponent(componentIndex + 3);
                c4 = parent.getComponent(componentIndex + 4);
                c5 = parent.getComponent(componentIndex + 5);
                updateLCBLCB(rowIndex, c0.getMinimumSize(), c1.getMinimumSize(), c2.getMinimumSize(),
                        c3.getMinimumSize(), c4.getMinimumSize(), c5.getMinimumSize());
                componentIndex = componentIndex + 5;
                break;
            }
        }
        complete();
        return new Dimension(this.totalWidth + insets.left + insets.right,
                totalHeight + (rowCount - 1) * this.rowGap + insets.top + insets.bottom);
    }
}

From source file:TileLayout.java

private void align(Dimension cont, Object cons[], Component comp) {
    int align = 0;
    Insets insets = null;/*w w  w .j a v  a  2  s .co m*/
    Rectangle tile = null, fixd = null;
    if (cons != null) {
        for (int i = 0; i < cons.length; i++) { // gather constraints
            if (cons[i] != null) {
                if (cons[i] instanceof Rectangle)
                    fixd = (Rectangle) cons[i];
                else if (cons[i] instanceof Insets)
                    insets = (Insets) cons[i];
                else if (cons[i] instanceof Integer)
                    align = ((Integer) cons[i]).intValue();
                else if (cons[i] instanceof Component)
                    tile = ((Component) cons[i]).getBounds();
            }
        }
    }
    if (tile == null)
        tile = new Rectangle(cont);
    Rectangle pref = new Rectangle(tile.getLocation(), comp.getPreferredSize());

    // perform component positioning:

    if ((align & 0x004000) != 0)
        pref.width = fixd.width;
    else if ((align & 0x008000) != 0)
        pref.width = (tile.width * fixd.width + 500) / 1000;
    else if ((align & 0x010000) != 0)
        pref.width = tile.width;

    if ((align & 0x080000) != 0)
        pref.height = fixd.height;
    else if ((align & 0x100000) != 0)
        pref.height = (tile.height * fixd.height + 500) / 1000;
    else if ((align & 0x200000) != 0)
        pref.height = tile.height;

    if ((align & 0x000001) != 0)
        pref.x -= pref.width;
    else if ((align & 0x000002) != 0)
        pref.x += (tile.width - pref.width >> 1);
    else if ((align & 0x000004) != 0)
        pref.x += tile.width - pref.width;
    else if ((align & 0x000008) != 0)
        pref.x += tile.width;
    else if ((align & 0x000010) != 0)
        pref.x += fixd.x;
    else if ((align & 0x000020) != 0)
        pref.x += (tile.width * fixd.x + 500) / 1000;

    if ((align & 0x000040) != 0)
        pref.y -= pref.height;
    else if ((align & 0x000080) != 0)
        pref.y += (tile.height - pref.height >> 1);
    else if ((align & 0x000100) != 0)
        pref.y += tile.height - pref.height;
    else if ((align & 0x000200) != 0)
        pref.y += tile.height;
    else if ((align & 0x000400) != 0)
        pref.y += fixd.y;
    else if ((align & 0x000800) != 0)
        pref.y += (tile.height * fixd.y + 500) / 1000;

    if ((align & 0x001000) != 0)
        pref.setBounds(0, pref.y, pref.x + pref.width, pref.height);
    else if ((align & 0x002000) != 0)
        pref.width = cont.width - pref.x;

    if ((align & 0x020000) != 0)
        pref.setBounds(pref.x, 0, pref.width, pref.y + pref.height);
    else if ((align & 0x040000) != 0)
        pref.height = cont.height - pref.y;

    if (insets != null) { // apply insets, if any:
        pref.x += insets.left;
        pref.y += insets.top;
        pref.width -= insets.left + insets.right;
        pref.height -= insets.top + insets.bottom;
    }

    // Note: this can cause surprising results, so use it, if you dare. :-)
    // Honour component minimum size:
    Dimension d = comp.getMinimumSize();
    if (pref.width < d.width)
        pref.width = d.width;
    if (pref.height < d.height)
        pref.height = d.height;

    comp.setBounds(pref); // now the tile is set!
}