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

public static void setAllOpaque(Container c, boolean opaque) {
    if (c instanceof JComponent) {
        ((JComponent) c).setOpaque(opaque);
        for (int i = 0; i < c.getComponentCount(); i++) {
            Component comp = c.getComponent(i);
            if (comp instanceof Container)
                setAllOpaque((Container) comp, opaque);
        }//from   www .j a va 2s. c  o m
    }
}

From source file:Main.java

/**
 * Attempts to locate the {@link Component} within the {@link Container}.
 * Returns {@code -1} if unable to locate in this {@link Container}. 
 * //w w w  . ja v a 2s.c o  m
 * @param container
 * @param child
 * @return
 */
public static int indexOf(Container container, Component child) {
    final int count = container instanceof JMenu ? ((JMenu) container).getMenuComponentCount()
            : container.getComponentCount();

    for (int i = 0; i < count; i++) {
        if (container instanceof JMenu) {
            if (((JMenu) container).getMenuComponent(i).equals(child)) {
                return i;
            }
        } else if (container.getComponent(i).equals(child)) {
            return i;
        }
    }
    return -1;
}

From source file:Main.java

/**
 * @param aContainer//from ww w  .  j a v  a  2s  .c  o  m
 * @param aComponentClass
 * @return
 */
private static Component findComponent(final Container aContainer,
        final Class<? extends Component> aComponentClass) {
    Component result = null;

    final int cnt = aContainer.getComponentCount();
    for (int i = 0; (result == null) && (i < cnt); i++) {
        final Component comp = aContainer.getComponent(i);
        if (aComponentClass.equals(comp.getClass())) {
            result = comp;
        } else if (comp instanceof Container) {
            result = findComponent((Container) comp, aComponentClass);
        }
    }
    return result;
}

From source file:Main.java

/**
 * Returns first component placed in the specified container which is
 * instance of specified class type or null if none found.
 *
 * @param container//  w  ww  .j ava  2 s.c o m
 *            container to look for component in
 * @param componentClass
 *            component class
 * @param recursive
 *            whether to check all subcontainers or not
 * @param <T>
 *            component class type
 * @return first component placed in the specified container which is
 *         instance of specified class type or null if none found
 */
public static <T extends Component> T getFirst(final Container container, final Class<T> componentClass,
        final boolean recursive) {
    for (int i = 0; i < container.getComponentCount(); i++) {
        final Component component = container.getComponent(i);
        if (componentClass.isInstance(component)) {
            return (T) component;
        }
        if (recursive) {
            if (component instanceof Container) {
                final T first = getFirst((Container) component, componentClass, recursive);
                if (first != null) {
                    return first;
                }
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Fills the array list with the all the components contained in the parent component and its sub-components.
 * /*from  ww w .  j  av a 2 s  .co m*/
 * @param list An <code>ArrayList</code>.
 * @param cmp The parent component.
 */
public static void fillComponentList(Component cmp, List<Component> list) {
    list.add(cmp);
    if (cmp instanceof Container) {
        Container cnt = (Container) cmp;
        for (int i = 0; i < cnt.getComponentCount(); i++) {
            fillComponentList(cnt.getComponent(i), list);
        }
    }
}

From source file:Main.java

protected static void fillAllDescendants(Container c, Class findClass, boolean deep, Collection results) {
    if (findClass.isInstance(c)) {
        results.add(c);/*from  w  w w.j  a v  a 2  s .  co m*/
        if (!deep)
            return;
    }
    for (int i = 0; i < c.getComponentCount(); i++) {
        Component comp = c.getComponent(i);
        if (comp instanceof Container)
            fillAllDescendants((Container) comp, findClass, deep, results);
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
private static <T> void addAllOfExclude(Container container, Class<T> clazz, List<T> all,
        Collection<? extends Component> exclude) {
    if (exclude.contains(container)) {
        return;/*from  w w  w . ja  va 2 s  .  co m*/
    }
    if (container.getClass().equals(clazz)) {
        all.add((T) container);
    }
    int count = container.getComponentCount();
    for (int i = 0; i < count; i++) {
        Component component = container.getComponent(i);
        if (exclude.contains(component)) {
            continue;
        }
        if (component instanceof Container) {
            addAllOfExclude((Container) component, clazz, all, exclude); // Recursive
        } else if (component.getClass().equals(clazz)) {
            all.add((T) component);
        }
    }
}

From source file:au.com.jwatmuff.eventmanager.util.GUIUtils.java

public static Map<String, Object> getComponentValues(Container parent) {
    Map<String, Object> values = new HashMap<String, Object>();

    for (int i = 0; i < parent.getComponentCount(); i++) {
        Component child = parent.getComponent(i);

        if (child instanceof Container)
            values.putAll(getComponentValues((Container) child));

        if (child.getName() != null) {
            String name = child.getName();

            if (child instanceof JTextField)
                values.put(name, ((JTextField) child).getText());
        }/*w w  w  . j ava2 s  . c o  m*/

    }

    return values;
}

From source file:org.esa.nest.dat.views.polarview.PolarCanvas.java

private static void paintComponents(Container c, Graphics g) {
    if (!c.isShowing())
        return;//from   w  w w.j a  va2  s .  co  m

    final int ncomponents = c.getComponentCount();
    final Rectangle clip = g.getClipBounds();

    int i = ncomponents - 1;
    while (i >= 0) {
        final Component component[] = c.getComponents();
        final Component comp = component[i];
        if (comp == null || !comp.isVisible())
            continue;
        final Rectangle bounds = comp.getBounds();
        Rectangle cr;
        if (clip == null)
            cr = new Rectangle(bounds);
        else
            cr = bounds.intersection(clip);
        if (cr.isEmpty())
            continue;

        final Graphics cg = g.create();
        cg.setClip(cr);
        cg.translate(bounds.x, bounds.y);
        try {
            comp.paint(cg);
        } catch (Throwable e) {
            //
        }

        cg.dispose();
        i--;
    }
}

From source file:Main.java

/**
 * Gets the current cursor position in the component or one of it's
 * subcomponents.//from  ww  w .  j  ava  2s  . c  o m
 *
 * @param component the component that is an EditorPane or some of its
 *      subcomponents is an EditorPane
 * @return current caret position
 */
public static int getCaretPosition(Container component) {
    if (component.getClass().getName().indexOf("EditorPane") >= 0) {
        try {
            java.lang.reflect.Method caretGetter = component.getClass().getMethod("getCaretPosition",
                    new Class[] {});
            Object result = caretGetter.invoke(component, new Object[] {});
            return ((Integer) result).intValue();
        } catch (Exception e) {
            e.printStackTrace();
            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) {
            int result = getCaretPosition((javax.swing.JComponent) childComponent);

            if (result >= 0) {
                return result;
            }
        }
    }

    return -1;
}