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

/**
 * Fills the array list with the all the components contained in the parent component and its sub-components.
 * /* www . ja  va2  s .c o 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:SpringBox.java

private static SpringLayout.Constraints getConstraintsForCell(int row, int col, Container parent, int cols) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component c = parent.getComponent(row * cols + col);
    return layout.getConstraints(c);
}

From source file:Main.java

/**
 * @param aContainer//from ww  w .j a v  a  2 s. c om
 * @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

@SuppressWarnings("unchecked")
private static <T> void addAllOf(Container container, Class<T> clazz, List<T> all) {
    int count = container.getComponentCount();

    if (container.getClass().equals(clazz)) {
        all.add((T) container);//  w  w w  .  ja va  2  s .  c om
    }

    for (int i = 0; i < count; i++) {
        Component component = container.getComponent(i);

        if (component instanceof Container) {
            addAllOf((Container) component, clazz, all); // 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());
        }/*from   w w  w.j a v  a2s .com*/

    }

    return values;
}

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.  j ava  2  s  . c o 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: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 . j a  va2s  . c  om*/
 * @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

/**
 * Gets the current cursor position in the component or one of it's
 * subcomponents.// www  .  j  av  a2s . co 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;
}

From source file:Main.java

/**
 * Obtains selected text/*from  w  w  w .j a  v  a2  s  .  c om*/
 *
 * @param component the component that is an EditorPane or some of its
 *      subcomponents is an EditorPane
 * @return current caret position
 */
public static String getSelectedText(Container component) {
    if (component.getClass().getName().indexOf("EditorPane") >= 0) {
        try {
            java.lang.reflect.Method caretGetter = component.getClass().getMethod("getSelectedText",
                    new Class[] {});
            Object result = caretGetter.invoke(component, new Object[] {});
            if (result == null) {
                return null;
            }
            return (String) result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Method invocation exception caught");
        }
    }

    for (int i = 0; i < component.getComponentCount(); i++) {
        Component childComponent = component.getComponent(i);
        if (childComponent instanceof JComponent) {
            return getSelectedText((JComponent) childComponent);
        }
    }

    return null;
}

From source file:Main.java

/**
 * Fills up the given ArrayList with all Components, matching to the given
 * class file, found downwards the given Container. The Container should be
 * a JRootPane for example.//from w ww .  j ava2  s . c om
 *
 * @param container
 *            Container that should be searched for The Component
 * @param recursive
 *            An empty ArrayList which will be filled up or null if a new
 *            ArrayList should be returned.
 * @return An array of the type specified with the className will be
 *         returned. If className is <code>null</code> an array over
 *         {@link Component} will be returned.
 */
private static ArrayList<Component> getAllComponentsRecursive(Container container,
        Class<? extends Component> classNames[], ArrayList<Component> recursive) {
    //is there no class name defined, just setup the default value.
    if (classNames == null || classNames.length == 0) {
        classNames = new Class[] { Component.class };
    }

    // No ArrayList, just create a new one.
    if (recursive == null) {
        // recursive = new Object[0];
        recursive = new ArrayList<>(100);
    }

    // No Container, nothing to do.
    if (container == null) {
        return recursive;
    }

    // Search for the component which is an instance of the Class specified
    // with the className parameter
    for (int i = 0; i < container.getComponentCount(); i++) {
        try {
            Component comp = container.getComponent(i);

            if (comp instanceof Container) {
                getAllComponentsRecursive((Container) comp, classNames, recursive);
            }

            for (int j = 0; j < classNames.length; j++) {
                if (classNames[j] == null || classNames[j].isInstance(comp)) {
                    recursive.add(comp);
                    break;
                }
            }
        } catch (Exception e) {
            // container.getComponent(i); can fail if it was removed.
        }
    }
    return recursive;
}