Example usage for java.awt Container getComponent

List of usage examples for java.awt Container getComponent

Introduction

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

Prototype

public Component getComponent(int n) 

Source Link

Document

Gets the nth component in this container.

Usage

From source file:net.sf.nmedit.jtheme.component.misc.CallDescriptor.java

private JTComponent getTarget() {
    if (target == null) {
        Container c = owner.getParent();
        if (c != null) {
            for (int i = c.getComponentCount() - 1; i >= 0; i--) {
                Component t = c.getComponent(i);
                if (t instanceof JTComponent && component.equals(t.getName())) {
                    target = (JTComponent) t;
                    break;
                }//  w w  w.  j a va 2s. com
            }
        }
    }
    return target;
}

From source file:CircleLayoutTest.java

public void setSizes(Container parent) {
    if (sizesSet)
        return;//from  w w w.ja  v  a  2s  . co  m
    int comCount = parent.getComponentCount();

    for (int i = 0; i < comCount; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            Dimension size = c.getPreferredSize();
            maxComponentWidth = Math.max(maxComponentWidth, size.width);
            maxComponentHeight = Math.max(maxComponentWidth, size.height);
            preferredHeight += size.height;
        }
    }
    preferredHeight += maxComponentHeight;
    preferredWidth = 2 * maxComponentWidth;
    minHeight = preferredHeight;
    minWidth = preferredWidth;
    sizesSet = true;
}

From source file:TextInputDemo.java

/**
 * Aligns the first <code>rows</code> * <code>cols</code>
 * components of <code>parent</code> in
 * a grid. Each component is as big as the maximum
 * preferred width and height of the components.
 * The parent is made just big enough to fit them all.
 *
 * @param rows number of rows//from   ww  w  . j  a va  2 s  . co m
 * @param cols number of columns
 * @param initialX x location to start the grid at
 * @param initialY y location to start the grid at
 * @param xPad x padding between cells
 * @param yPad y padding between cells
 */
public static void makeGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad,
        int yPad) {
    SpringLayout layout;
    try {
        layout = (SpringLayout) parent.getLayout();
    } catch (ClassCastException exc) {
        System.err.println("The first argument to makeGrid must use SpringLayout.");
        return;
    }

    Spring xPadSpring = Spring.constant(xPad);
    Spring yPadSpring = Spring.constant(yPad);
    Spring initialXSpring = Spring.constant(initialX);
    Spring initialYSpring = Spring.constant(initialY);
    int max = rows * cols;

    //Calculate Springs that are the max of the width/height so that all
    //cells have the same size.
    Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
    Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
    for (int i = 1; i < max; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));

        maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
        maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
    }

    //Apply the new width/height Spring. This forces all the
    //components to have the same size.
    for (int i = 0; i < max; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));

        cons.setWidth(maxWidthSpring);
        cons.setHeight(maxHeightSpring);
    }

    //Then adjust the x/y constraints of all the cells so that they
    //are aligned in a grid.
    SpringLayout.Constraints lastCons = null;
    SpringLayout.Constraints lastRowCons = null;
    for (int i = 0; i < max; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
        if (i % cols == 0) { //start of new row
            lastRowCons = lastCons;
            cons.setX(initialXSpring);
        } else { //x position depends on previous component
            cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), xPadSpring));
        }

        if (i / cols == 0) { //first row
            cons.setY(initialYSpring);
        } else { //y position depends on previous row
            cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), yPadSpring));
        }
        lastCons = cons;
    }

    //Set the parent's size.
    SpringLayout.Constraints pCons = layout.getConstraints(parent);
    pCons.setConstraint(SpringLayout.SOUTH,
            Spring.sum(Spring.constant(yPad), lastCons.getConstraint(SpringLayout.SOUTH)));
    pCons.setConstraint(SpringLayout.EAST,
            Spring.sum(Spring.constant(xPad), lastCons.getConstraint(SpringLayout.EAST)));
}

From source file:XYLayout.java

public void layoutContainer(Container target) {
    Insets insets = target.getInsets();
    int count = target.getComponentCount();
    for (int i = 0; i < count; i++) {
        Component component = target.getComponent(i);
        if (component.isVisible()) {
            Rectangle r = getComponentBounds(component, true);
            component.setBounds(insets.left + r.x, insets.top + r.y, r.width, r.height);
        }/*from w ww. ja  va2 s .c o  m*/
    }

}

From source file:CenterLayout.java

/**
 * Returns the minimum size./*from  ww  w  .j av a2s.  com*/
 * 
 * @param parent
 *          the parent.
 * 
 * @return the minimum size.
 */
public Dimension minimumLayoutSize(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        if (parent.getComponentCount() > 0) {
            final Component component = parent.getComponent(0);
            final Dimension d = component.getMinimumSize();
            return new Dimension(d.width + insets.left + insets.right, d.height + insets.top + insets.bottom);
        } else {
            return new Dimension(insets.left + insets.right, insets.top + insets.bottom);
        }
    }

}

From source file:CenterLayout.java

/**
 * Returns the preferred size.//  w  w  w.  j  av  a2s. com
 * 
 * @param parent
 *          the parent.
 * 
 * @return the preferred size.
 */
public Dimension preferredLayoutSize(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        if (parent.getComponentCount() > 0) {
            final Component component = parent.getComponent(0);
            final Dimension d = component.getPreferredSize();
            return new Dimension((int) d.getWidth() + insets.left + insets.right,
                    (int) d.getHeight() + insets.top + insets.bottom);
        } else {
            return new Dimension(insets.left + insets.right, insets.top + insets.bottom);
        }
    }

}

From source file:SquareLayout.java

/**
 * Returns the sole child of the specified container, or <code>null</code> if
 * none. Throws an <code>IllegalStateException</code> if there is more than
 * one child.//from w w w . j  a  va2  s  .c  o  m
 */

private Component getChild(Container c) {
    int childCount = c.getComponentCount();
    if (childCount > 1)
        throw new IllegalStateException("May not layout more than one component");
    else if (childCount == 0)
        return null;
    else
        return c.getComponent(childCount - 1);
}

From source file:com.equitysoft.cellspark.ProportionalLayout.java

private Dimension layoutSize(Container parent, boolean minimum) {
    Dimension dim = new Dimension(0, 0);
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            int maxhgt = 0;
            if (c.isVisible()) {
                Dimension d = (minimum) ? c.getMinimumSize() : c.getPreferredSize();
                dim.width += d.width;//from w w w.  j  a  va 2s  .  c  o  m
                if (d.height > dim.height)
                    dim.height = d.height;
            }
            cnt++;
            if (cnt == num)
                break;
        }
    }
    Insets insets = parent.getInsets();
    dim.width += insets.left + insets.right;
    dim.height += insets.top + insets.bottom;
    return dim;
}

From source file:VerticalLayout.java

private Dimension layoutSize(Container parent, boolean minimum) {
    Dimension dim = new Dimension(0, 0);
    Dimension d;//w  ww .j  a  v a 2s  .co m
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            if (c.isVisible()) {
                d = minimum ? c.getMinimumSize() : c.getPreferredSize();
                dim.width = Math.max(dim.width, d.width);
                dim.height += d.height;
                if (i > 0)
                    dim.height += vgap;
            }
        }
    }
    Insets insets = parent.getInsets();
    dim.width += insets.left + insets.right;
    dim.height += insets.top + insets.bottom + vgap + vgap;
    return dim;
}

From source file:CenterLayout.java

/**
 * Lays out the components.//from   w w  w. ja va  2s  .c om
 * 
 * @param parent
 *          the parent.
 */
public void layoutContainer(final Container parent) {

    synchronized (parent.getTreeLock()) {
        if (parent.getComponentCount() > 0) {
            final Insets insets = parent.getInsets();
            final Dimension parentSize = parent.getSize();
            final Component component = parent.getComponent(0);
            final Dimension componentSize = component.getPreferredSize();
            final int xx = insets.left
                    + (Math.max((parentSize.width - insets.left - insets.right - componentSize.width) / 2, 0));
            final int yy = insets.top + (Math
                    .max((parentSize.height - insets.top - insets.bottom - componentSize.height) / 2, 0));
            component.setBounds(xx, yy, componentSize.width, componentSize.height);
        }
    }

}