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:com.equitysoft.cellspark.ProportionalLayout.java

/**
 * Lays out the container./*w  w w .  j  a  va 2s . co  m*/
 */
public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        Dimension pd = parent.getSize();
        //do layout
        int cnt = 0;
        int totalwid = pd.width - insets.left - insets.right;
        int x = insets.left;
        int cumwid = 0;
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            int wid = proportions[i] * totalwid / total;
            c.setBounds(x, insets.top, wid, pd.height - insets.bottom - insets.top);
            x += wid;
            cnt++;
            if (cnt == num)
                break;
        }
    }
}

From source file:ColumnLayout.java

/**
 * The method that actually performs the layout. Called by the Container
 *///w w  w .  ja  v a 2s.  co m
public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    Dimension parent_size = parent.getSize();
    Component kid;
    int nkids = parent.getComponentCount();
    int x0 = insets.left + margin_width; // The base X position
    int x;
    int y = insets.top + margin_height; // Start at the top of the column

    for (int i = 0; i < nkids; i++) { // Loop through the kids
        kid = parent.getComponent(i); // Get the kid
        if (!kid.isVisible())
            continue; // Skip hidden ones
        Dimension pref = kid.getPreferredSize(); // How big is it?
        switch (alignment) { // Compute X coordinate
        default:
        case LEFT:
            x = x0;
            break;
        case CENTER:
            x = (parent_size.width - pref.width) / 2;
            break;
        case RIGHT:
            x = parent_size.width - insets.right - margin_width - pref.width;
            break;
        }
        // Set the size and position of this kid
        kid.setBounds(x, y, pref.width, pref.height);
        y += pref.height + spacing; // Get Y position of the next one
    }
}

From source file:nl.xs4all.home.freekdb.b52reader.gui.MainGuiTest.java

private Component findComponent(Container parent, Class<?> searchClass) {
    Component result = null;//  w  w w.j  a va2s  .  c om

    int componentIndex = 0;
    while (componentIndex < parent.getComponentCount() && result == null) {
        Component component = parent.getComponent(componentIndex);

        if (searchClass.isInstance(component)) {
            result = component;
        } else if (component instanceof Container) {
            result = findComponent((Container) component, searchClass);
        }

        componentIndex++;
    }

    return result;
}

From source file:ome.formats.importer.gui.FileQueueChooser.java

/**
 * Get all JLists and JTables if the LAF uses lists/tables
 * /*from w ww . j av a2s  . c  o  m*/
 * @param fileChooser fileChooser
 * @return fileListObjects
 */
public Component[] getFileListObjects(JFileChooser fileChooser) {
    Vector<Component> v = new Vector<Component>();
    Stack<Component> s = new Stack<Component>();
    s.push(fileChooser);
    while (!s.isEmpty()) {
        Component c = (Component) s.pop();

        if (c instanceof Container) {
            Container d = (Container) c;
            for (int i = 0; i < d.getComponentCount(); i++) {

                if (d.getComponent(i) instanceof JTable) {
                    v.add(d.getComponent(i));
                } else
                    s.push(d.getComponent(i));
            }
        }
    }
    Component[] arr = new Component[v.size()];
    for (int i = 0; i < arr.length; i++)
        arr[i] = v.get(i);

    return arr;
}

From source file:VerticalFlowLayout.java

/**
 *  Description of the Method//from w  w  w  .  jav a  2s .  c o m
 *
 *@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/*w  w  w. j  a v a2 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;
    }
}

From source file:CircleLayoutDemo.java

/**
 * Arranges the parent's Component objects in either an Ellipse or a Circle.
 * Ellipse is not yet implemented.//w ww  . j a  v a 2s .c o  m
 */
public void layoutContainer(Container parent) {
    int x, y, w, h, s, c;
    int n = parent.getComponentCount();
    double parentWidth = parent.getSize().width;
    double parentHeight = parent.getSize().height;
    Insets insets = parent.getInsets();
    int centerX = (int) (parentWidth - (insets.left + insets.right)) / 2;
    int centerY = (int) (parentHeight - (insets.top + insets.bottom)) / 2;

    Component comp = null;
    Dimension compPS = null;
    if (n == 1) {
        comp = parent.getComponent(0);
        x = centerX;
        y = centerY;
        compPS = comp.getPreferredSize();
        w = compPS.width;
        h = compPS.height;
        comp.setBounds(x, y, w, h);
    } else {
        double r = (Math.min(parentWidth - (insets.left + insets.right),
                parentHeight - (insets.top + insets.bottom))) / 2;
        r *= 0.75; // Multiply by .75 to account for extreme right and bottom
                   // Components
        for (int i = 0; i < n; i++) {
            comp = parent.getComponent(i);
            compPS = comp.getPreferredSize();
            if (isCircle) {
                c = (int) (r * Math.cos(2 * i * Math.PI / n));
                s = (int) (r * Math.sin(2 * i * Math.PI / n));
            } else {
                c = (int) ((centerX * 0.75) * Math.cos(2 * i * Math.PI / n));
                s = (int) ((centerY * 0.75) * Math.sin(2 * i * Math.PI / n));
            }
            x = c + centerX;
            y = s + centerY;

            w = compPS.width;
            h = compPS.height;

            comp.setBounds(x, y, w, h);
        }
    }

}

From source file:LCBLayout.java

/**
 * Returns the preferred size using this layout manager.
 *
 * @param parent  the parent.//from  ww w.  j  a  v  a2s .  c  o  m
 *
 * @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  a  v  a  2  s . com
 *
 * @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:Main.java

public static Point positionToClickPoint(Container component, int caretPosition, Container invokedIn) {
    if (component == null) {
        return null;
    }/*from w  w w  .j  a  va  2  s . c  o  m*/

    //System.err.println("Checking: " + component.getClass().getName());
    if (component.getClass().getName().indexOf("EditorPane") >= 0) {
        try {
            java.lang.reflect.Method pointGetter = component.getClass().getMethod("modelToView",
                    new Class[] { Integer.TYPE });
            Rectangle rec = (Rectangle) pointGetter.invoke(component,
                    new Object[] { new Integer(caretPosition) });
            //System.err.println("Before: " + (int)rec.getY());
            // FIXME: somehow it fails here to convert point from scrollable component
            Point point = SwingUtilities.convertPoint(component, (int) rec.getX(), (int) rec.getY() + 10,
                    invokedIn);
            // FIXME: ugly hack :(
            if (point.getY() > 1024) {
                point = new Point((int) point.getX(), 250);
            }
            //System.err.println("After: " + (int)point.getY());
            return point;
        } catch (Exception e) {
            System.err.println("Method invocation exception caught");
            e.printStackTrace();

            //FIXME: BUG
            return null;
            //throw new RuntimeException("Method invocation exception caught");
        }
    }

    for (int i = 0; i < component.getComponentCount(); i++) {
        java.awt.Component childComponent = component.getComponent(i);
        if (childComponent instanceof javax.swing.JComponent) {
            Point point = positionToClickPoint((javax.swing.JComponent) childComponent, caretPosition,
                    invokedIn);
            if (point != null) {
                return point;
            }
        }
    }

    return null;
}