Example usage for java.awt Container getComponentCount

List of usage examples for java.awt Container getComponentCount

Introduction

In this page you can find the example usage for java.awt Container getComponentCount.

Prototype

public int getComponentCount() 

Source Link

Document

Gets the number of components in this panel.

Usage

From source file:XYLayout.java

Dimension getLayoutSize(Container target, boolean doPreferred) {
    Dimension dim = new Dimension(0, 0);
    if (width <= 0 || height <= 0) {
        int count = target.getComponentCount();
        for (int i = 0; i < count; i++) {
            Component component = target.getComponent(i);
            if (component.isVisible()) {
                Rectangle r = getComponentBounds(component, doPreferred);
                dim.width = Math.max(dim.width, r.x + r.width);
                dim.height = Math.max(dim.height, r.y + r.height);
            }/*from   ww w . j a  v  a  2  s  .  co  m*/
        }

    }
    if (width > 0)
        dim.width = width;
    if (height > 0)
        dim.height = height;
    Insets insets = target.getInsets();
    dim.width += insets.left + insets.right;
    dim.height += insets.top + insets.bottom;
    return dim;
}

From source file:edu.ku.brc.af.ui.forms.validation.ValSpinner.java

/**
 * Sets the spinner to the proper color.
 *///w w w. j ava2  s .  c  o  m
protected JTextField getTextField(final Container container) {
    for (int i = 0; i < container.getComponentCount(); i++) {
        Component c = container.getComponent(i);
        if (c instanceof JTextField) {
            return (JTextField) c;

        } else if (c instanceof Container) {
            JTextField tf = getTextField((Container) c);
            if (tf != null) {
                return tf;
            }
        }
    }
    return null;
}

From source file:GraphPaperLayout.java

/**
 * Algorithm for calculating the largest minimum or preferred cell size.
 * <P>/* w w w.  ja v a 2  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  .java2 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 = (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:LCBLayout.java

/**
 * Returns the preferred size using this layout manager.
 *
 * @param parent  the parent.//  w  w w .j  av a  2s .c om
 *
 * @return the preferred size using this layout manager.
*/
public Dimension preferredLayoutSize(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        final int ncomponents = parent.getComponentCount();
        final int nrows = ncomponents / COLUMNS;
        for (int c = 0; c < COLUMNS; c++) {
            for (int r = 0; r < nrows; r++) {
                final Component component = parent.getComponent(r * COLUMNS + c);
                final Dimension d = component.getPreferredSize();
                if (this.colWidth[c] < d.width) {
                    this.colWidth[c] = d.width;
                }
                if (this.rowHeight[r] < d.height) {
                    this.rowHeight[r] = d.height;
                }
            }
        }
        int totalHeight = this.vGap * (nrows - 1);
        for (int r = 0; r < nrows; r++) {
            totalHeight = totalHeight + this.rowHeight[r];
        }
        final int totalWidth = this.colWidth[0] + this.labelGap + this.colWidth[1] + this.buttonGap
                + this.colWidth[2];
        return new Dimension(insets.left + insets.right + totalWidth + this.labelGap + this.buttonGap,
                insets.top + insets.bottom + totalHeight + this.vGap);
    }

}

From source file:LCBLayout.java

/**
 * Returns the minimum size using this layout manager.
 *
 * @param parent  the parent./*from   w  ww  . j  av  a2s  . c o m*/
 *
 * @return the minimum size using this layout manager.
 */
public Dimension minimumLayoutSize(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        final int ncomponents = parent.getComponentCount();
        final int nrows = ncomponents / COLUMNS;
        for (int c = 0; c < COLUMNS; c++) {
            for (int r = 0; r < nrows; r++) {
                final Component component = parent.getComponent(r * COLUMNS + c);
                final Dimension d = component.getMinimumSize();
                if (this.colWidth[c] < d.width) {
                    this.colWidth[c] = d.width;
                }
                if (this.rowHeight[r] < d.height) {
                    this.rowHeight[r] = d.height;
                }
            }
        }
        int totalHeight = this.vGap * (nrows - 1);
        for (int r = 0; r < nrows; r++) {
            totalHeight = totalHeight + this.rowHeight[r];
        }
        final int totalWidth = this.colWidth[0] + this.labelGap + this.colWidth[1] + this.buttonGap
                + this.colWidth[2];
        return new Dimension(insets.left + insets.right + totalWidth + this.labelGap + this.buttonGap,
                insets.top + insets.bottom + totalHeight + this.vGap);
    }

}

From source file:LCBLayout.java

/**
 * Lays out the components.//  w w  w. j  a  v a 2  s.  c o m
 *
 * @param parent  the parent.
 */
public void layoutContainer(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        final int ncomponents = parent.getComponentCount();
        final int nrows = ncomponents / COLUMNS;
        for (int c = 0; c < COLUMNS; c++) {
            for (int r = 0; r < nrows; r++) {
                final Component component = parent.getComponent(r * COLUMNS + c);
                final Dimension d = component.getPreferredSize();
                if (this.colWidth[c] < d.width) {
                    this.colWidth[c] = d.width;
                }
                if (this.rowHeight[r] < d.height) {
                    this.rowHeight[r] = d.height;
                }
            }
        }
        int totalHeight = this.vGap * (nrows - 1);
        for (int r = 0; r < nrows; r++) {
            totalHeight = totalHeight + this.rowHeight[r];
        }
        final int totalWidth = this.colWidth[0] + this.colWidth[1] + this.colWidth[2];

        // adjust the width of the second column to use up all of parent
        final int available = parent.getWidth() - insets.left - insets.right - this.labelGap - this.buttonGap;
        this.colWidth[1] = this.colWidth[1] + (available - totalWidth);

        // *** DO THE LAYOUT ***
        int x = insets.left;
        for (int c = 0; c < COLUMNS; c++) {
            int y = insets.top;
            for (int r = 0; r < nrows; r++) {
                final int i = r * COLUMNS + c;
                if (i < ncomponents) {
                    final Component component = parent.getComponent(i);
                    final Dimension d = component.getPreferredSize();
                    final int h = d.height;
                    final int adjust = (this.rowHeight[r] - h) / 2;
                    parent.getComponent(i).setBounds(x, y + adjust, this.colWidth[c], h);
                }
                y = y + this.rowHeight[r] + this.vGap;
            }
            x = x + this.colWidth[c];
            if (c == 0) {
                x = x + this.labelGap;
            }
            if (c == 1) {
                x = x + this.buttonGap;
            }
        }

    }

}

From source file:Main.java

public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    int maxWidth = parent.getWidth() - (insets.left + insets.right);
    int maxHeight = parent.getHeight() - (insets.top + insets.bottom);
    int nComps = parent.getComponentCount();
    int previousWidth = 0, previousHeight = 0;
    int x = 0, y = insets.top;
    int rowh = 0, start = 0;
    int xFudge = 0, yFudge = 0;
    boolean oneColumn = false;

    // Go through the components' sizes, if neither
    // preferredLayoutSize nor minimumLayoutSize has
    // been called.
    if (sizeUnknown) {
        setSizes(parent);/*  w w w . j a  v a  2s.com*/
    }

    if (maxWidth <= minWidth) {
        oneColumn = true;
    }

    if (maxWidth != preferredWidth) {
        xFudge = (maxWidth - preferredWidth) / (nComps - 1);
    }

    if (maxHeight > preferredHeight) {
        yFudge = (maxHeight - preferredHeight) / (nComps - 1);
    }

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

            // increase x and y, if appropriate
            if (i > 0) {
                if (!oneColumn) {
                    x += previousWidth / 2 + xFudge;
                }
                y += previousHeight + vgap + yFudge;
            }

            // If x is too large,
            if ((!oneColumn) && (x + d.width) > (parent.getWidth() - insets.right)) {
                // reduce x to a reasonable number.
                x = parent.getWidth() - insets.bottom - d.width;
            }

            // If y is too large,
            if ((y + d.height) > (parent.getHeight() - insets.bottom)) {
                // do nothing.
                // Another choice would be to do what we do to x.
            }

            // Set the component's size and position.
            c.setBounds(x, y, d.width, d.height);

            previousWidth = d.width;
            previousHeight = d.height;
        }
    }
}

From source file:VerticalFlowLayout.java

/**
 *  Description of the Method//  www.java2s . c  om
 *
 *@param  target  Description of Parameter
 *@return         Description of the Returned Value
 */
public Dimension preferredLayoutSize(Container target) {
    synchronized (target.getTreeLock()) {
        Dimension dim = new Dimension(0, 0);
        int nmembers = target.getComponentCount();
        boolean firstVisibleComponent = true;

        for (int ii = 0; ii < nmembers; ii++) {
            Component m = target.getComponent(ii);
            if (m.isVisible()) {
                Dimension d = m.getPreferredSize();
                dim.width = Math.max(dim.width, d.width);
                if (firstVisibleComponent) {
                    firstVisibleComponent = false;
                } else {
                    dim.height += _vgap;
                }
                dim.height += d.height;
            }
        }
        Insets insets = target.getInsets();
        dim.width += insets.left + insets.right + _hgap * 2;
        dim.height += insets.top + insets.bottom + _vgap * 2;
        return dim;
    }
}

From source file:VerticalFlowLayout.java

/**
 *  Description of the Method//from www  .  j a  va2  s.  c  o  m
 *
 *@param  target  Description of Parameter
 *@return         Description of the Returned Value
 */
public Dimension minimumLayoutSize(Container target) {
    synchronized (target.getTreeLock()) {
        Dimension dim = new Dimension(0, 0);
        int nmembers = target.getComponentCount();
        boolean firstVisibleComponent = true;

        for (int ii = 0; ii < nmembers; ii++) {
            Component m = target.getComponent(ii);
            if (m.isVisible()) {
                Dimension d = m.getPreferredSize();
                dim.width = Math.max(dim.width, d.width);
                if (firstVisibleComponent) {
                    firstVisibleComponent = false;
                } else {
                    dim.height += _vgap;
                }
                dim.height += d.height;
            }
        }
        Insets insets = target.getInsets();
        dim.width += insets.left + insets.right + _hgap * 2;
        dim.height += insets.top + insets.bottom + _vgap * 2;
        return dim;
    }
}