Java tutorial
//package com.java2s; /** * A collection of small utilities for component management and reflection handling. * <p/> * <hr/> Copyright 2006-2012 Torsten Heup * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ import java.awt.*; import java.util.Collection; import java.util.LinkedList; public class Main { /** * This method will collection all child components of the given container. Unlike Component#getComponent, this * method will recursively delve into the depths of the component hierarchy, gathering all descendants rather than * the direct children. * <p/> * Optionally, you may declare a filter for the children being collected by defining a list of Classes for all types * you want included. <code> getChildComponents(myContentPane, JLabel.class) </code> would return all JLabels in the * given container. This works for superclasses as well: <code> getChildComponents(myContentPane, * JToggleButton.class) </code> would return all instances of JRadioButton and JCheckBox (and, of course, all other * JToggleButton implementations). Finally, filtering also works for interfaces, so the specified classes do not * necessarily have to extend Component: <code> getChildComponents(myContentPane, Accessible.class) </code> returns * all components implementing the Accessible interface. * * @param container The container which's children are being collected. Non-null. * @param filter The filter to apply to the list of child components. May be null. * @return All descendants of the given container. The results may optionally be filtered by the 'filter' array. */ public static <T extends Component> Collection<T> getChildComponents(final Container container, final Class<T>... filter) { if (container == null) throw new IllegalArgumentException("Parameter 'parent' must not be null!"); final LinkedList<T> children = new LinkedList<T>(); getChildComponents(container, filter, children); return children; } /** * The internal, recursive implementation of getChildComponents(Container, Class...). * * @param parent Container which's children are being gathered. * @param filter The class filter applied, may be null. * @param resultList The list of gathered child components. */ protected static void getChildComponents(final Container parent, final Class[] filter, final Collection resultList) { final Component[] components = parent.getComponents(); for (Component c : components) { if (filter.length == 0) resultList.add(c); else { for (Class f : filter) { if (f.isAssignableFrom(c.getClass())) { resultList.add(c); break; } } } if (c instanceof Container) getChildComponents((Container) c, filter, resultList); } } }