Example usage for java.awt Container getComponents

List of usage examples for java.awt Container getComponents

Introduction

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

Prototype

public Component[] getComponents() 

Source Link

Document

Gets all the components in this container.

Usage

From source file:GroupRadio.java

public static Enumeration getSelectedElements(Container container) {
    Vector selections = new Vector();
    Component components[] = container.getComponents();
    for (int i = 0, n = components.length; i < n; i++) {
        if (components[i] instanceof AbstractButton) {
            AbstractButton button = (AbstractButton) components[i];
            if (button.isSelected()) {
                selections.addElement(button.getText());
            }/*from  w  ww  . jav  a 2 s. c o m*/
        }
    }
    return selections.elements();
}

From source file:Main.java

/**
 * Enables or disables all children of the given container.
 *
 * @param container the container/*from  www  .j  a va  2s.c om*/
 * @param enable if true, the components are enabled, otherwise they are disabled
 */
public static void enableContainerChildren(Container container, boolean enable) {
    if (container == null)
        return;
    for (Component comp : container.getComponents()) {
        try {
            comp.setEnabled(enable);
            ((JComponent) comp).setOpaque(enable);
            if (comp instanceof Container)
                enableContainerChildren((Container) comp, enable);
        } catch (ClassCastException e) {
            // ignore component
            continue;
        }
    }
}

From source file:Main.java

private static void setForegroundOrBackgroundDeep(final boolean doForeground, Component component, Color color,
        Class[] excludedClasses, boolean processContentsOfExcludedContainers) {

    // If this component one that should be excluded?
    boolean excluded = false;
    if (excludedClasses != null) {
        for (int i = 0; i < excludedClasses.length && !excluded; i++) {
            if (excludedClasses[i].isInstance(component)) {
                excluded = true;//from w  w  w  .  ja v  a 2 s.co  m
            }
        }
    }

    // If not exluded, set the component's foreground or background.
    if (!excluded) {
        if (doForeground) {
            component.setForeground(color);
        } else {
            component.setBackground(color);
        }
    }

    // Recursively process the contents of containers.
    if ((!excluded || processContentsOfExcludedContainers) && (component instanceof Container)) {
        Container container = (Container) component;
        Component[] children = container.getComponents();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                setForegroundOrBackgroundDeep(doForeground, children[i], color, excludedClasses,
                        processContentsOfExcludedContainers);
            }
        }
    }
}

From source file:Main.java

static public <T> void findDescendantOfType(List<T> holder, Container parent, Class<T> type) {
    if (type.isInstance(parent)) {
        holder.add((T) parent);//www . j  a va  2 s .  c om
    }
    for (Component child : parent.getComponents()) {
        if (child instanceof Container) {
            findDescendantOfType(holder, (Container) child, type);
        } else {
            if (type.isInstance(child)) {
                holder.add((T) child);
            }
        }
    }
}

From source file:SpringDemo4.java

public static void setContainerSize(Container parent, int pad) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component[] components = parent.getComponents();
    Spring maxHeightSpring = Spring.constant(0);
    SpringLayout.Constraints pCons = layout.getConstraints(parent);

    // Set the container's right edge to the right edge
    // of its rightmost component + padding.
    Component rightmost = components[components.length - 1];
    SpringLayout.Constraints rCons = layout.getConstraints(rightmost);
    pCons.setConstraint(SpringLayout.EAST,
            Spring.sum(Spring.constant(pad), rCons.getConstraint(SpringLayout.EAST)));

    // Set the container's bottom edge to the bottom edge
    // of its tallest component + padding.
    for (int i = 0; i < components.length; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(components[i]);
        maxHeightSpring = Spring.max(maxHeightSpring, cons.getConstraint(SpringLayout.SOUTH));
    }/* w  w w .j  a  v a 2s.  co m*/
    pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(pad), maxHeightSpring));
}

From source file:Main.java

/**
 * Sets the background color for the specified container and all the
 * JPanel, JLabel, JCheckBox, JComboBox, JTextField, JRadioButton, and
 * JScrollPane components that it contains to the same color.
 * //  w  w w.  j  av  a2s.  c  o  m
 * @param c the container to set the background color of.
 * @param bg the background color.
 */
public static void setBackground(Container c, Color bg) {
    c.setBackground(bg);
    Component[] children = c.getComponents();
    if (children == null) {
        return;
    }
    Component child;
    int len = children.length;
    if (bg != null) {
        for (int i = 0; i < len; i++) {
            child = children[i];
            if (!bg.equals(child.getBackground())
                    && ((child instanceof JPanel) || (child instanceof JLabel) || (child instanceof JCheckBox)
                            || (child instanceof JComboBox) || (child instanceof JTextField)
                            || (child instanceof JRadioButton) || (child instanceof JScrollPane))) {
                child.setBackground(bg);
            }
        }
    } else {
        // Null background color case
        for (int i = 0; i < len; i++) {
            child = children[i];
            if ((child.getBackground() != null) && ((child instanceof JPanel) || (child instanceof JLabel)
                    || (child instanceof JCheckBox))) {
                child.setBackground(null);
            } //end if
        } // end for
    } //end if else
}

From source file:Main.java

public static List<JComponent> getComponents(Container owner, Class<?> clazz, boolean onlyVisible) {
    List<JComponent> list = new ArrayList<JComponent>();
    for (Component c : owner.getComponents()) {
        if (clazz.isInstance(c) && (!onlyVisible || c.isShowing()))
            list.add((JComponent) c);
        else if (c instanceof JComponent) {
            for (JComponent b : getComponents((JComponent) c, clazz, onlyVisible))
                list.add(b);/*ww w .j  av a 2 s.  c om*/
        }
    }
    return list;
}

From source file:Main.java

public static void enableAll(Container container, boolean flag) {
    if (container == null)
        return;//from   w  w w. jav a  2 s.  c om
    container.setEnabled(flag);
    Component acomponent[] = container.getComponents();
    if (acomponent == null)
        return;
    for (int i = 0; i < acomponent.length; i++)
        if (acomponent[i] instanceof Container)
            enableAll((Container) acomponent[i], flag);
        else
            acomponent[i].setEnabled(flag);

}

From source file:DebugUtilities.java

public static void printContainerComponents(java.awt.Container cont, PrintWriter writer, String prefix,
        boolean recurse) {
    java.awt.Component[] comps = cont.getComponents();

    if (comps.length < 1) {
        writer.println(prefix + "Contains no components.");
    }/*from w  ww.j  a v a 2  s. co  m*/

    for (int i = 0; i < comps.length; ++i) {
        DebugUtilities.printClassHierarchy(comps[i].getClass(), writer, prefix + "[" + i + "]");

        if (recurse) {
            Class c = java.awt.Container.class;
            if (c.isAssignableFrom(comps[i].getClass())) {
                DebugUtilities.printContainerComponents((java.awt.Container) comps[i], writer,
                        (prefix + "[" + i + "] "), recurse);
            }
        }
    }
}

From source file:Main.java

public static JButton getButton(Container container, String text) {
    JButton btn = null;//from ww  w  .  j a  va2s  .c o m
    List<Container> children = new ArrayList<>(25);
    for (Component child : container.getComponents()) {
        if (child instanceof JButton) {
            JButton button = (JButton) child;
            if (text.equals(button.getText())) {
                btn = button;
                break;
            }
        } else if (child instanceof Container) {
            children.add((Container) child);
        }
    }
    if (btn == null) {
        for (Container cont : children) {
            JButton button = getButton(cont, text);
            if (button != null) {
                btn = button;
                break;
            }
        }
    }
    return btn;
}