Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 BSI Business Systems Integration AG. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * BSI Business Systems Integration AG - initial API and implementation ******************************************************************************/ import java.awt.Component; import java.awt.Container; import java.util.List; public class Main { @SuppressWarnings("unchecked") private static <T extends Component> void findChildComponentsRec(Component parent, Class<T> type, List<T> list, int maxCount) { if (type.isAssignableFrom(parent.getClass())) { list.add((T) parent); if (list.size() >= maxCount) { return; } } // if (parent instanceof Container) { for (Component c : ((Container) parent).getComponents()) { findChildComponentsRec(c, type, list, maxCount); if (list.size() >= maxCount) { return; } } } } }