Java JComponent Container getComponentsOfType(JComponent base, Class type)

Here you can find the source of getComponentsOfType(JComponent base, Class type)

Description

gather all components which are instances of a specific class

License

Apache License

Parameter

Parameter Description
base non-null base tostart
type non-null type - must be a subtyoe of JCOmponent

Return

non-null array of implementing components

Declaration

public static JComponent[] getComponentsOfType(JComponent base, Class type) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.swing.*;

import java.awt.*;

import java.lang.reflect.Array;

import java.util.*;
import java.util.List;

public class Main {
    /**/*from w w w . ja va 2  s.  c  o m*/
     * gather all components which are instances of a specific class
     *
     * @param base non-null base tostart
     * @param type non-null type - must be a subtyoe of JCOmponent
     * @return non-null array of implementing components
     */
    public static JComponent[] getComponentsOfType(JComponent base, Class type) {
        List<JComponent> holder = new ArrayList<JComponent>();
        accumulateComponentsOfType(base, type, holder);
        JComponent[] ret = (JComponent[]) Array.newInstance(type, holder.size());
        holder.toArray(ret);
        return ret;
    }

    protected static void accumulateComponentsOfType(JComponent base, Class type, List<JComponent> holder) {
        Component[] components = base.getComponents();
        for (int i = 0; i < components.length; i++) {
            Component component = components[i];
            if (type.isInstance(component))
                holder.add((JComponent) component);
        }
    }
}

Related

  1. getChildJComponents(Container container)
  2. getClipboard(JComponent c)
  3. getComponent(JComponent container, String name)
  4. getComponent(JComponent container, String name)
  5. getComponentByName(boolean enableDeepSearch, JComponent rootComponent, String name)
  6. getComponentState(JComponent c)
  7. getDataFlavors(JComponent component)
  8. getDeepestEmptyComponentAt(JComponent parent, Point location)
  9. getFirstChildComponentOfType(Component component, Class childComponentType)