Java tutorial
//package com.java2s; /* * Copyright (c) 2005-2016 Vincent Vandenschrick. All rights reserved. * * This file is part of the Jspresso framework. * * Jspresso is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Jspresso is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Jspresso. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.Component; import java.awt.Container; import javax.swing.JComponent; public class Main { /** * Retrieves the first contained component of a certain type. * * @param component * the component to start from. * @param childComponentType * the type of the component to look for. * @return the first contained component of the looked for type or null if * none. */ public static Component getFirstChildComponentOfType(Component component, Class<? extends JComponent> childComponentType) { if (childComponentType.isAssignableFrom(component.getClass())) { return component; } if (component instanceof Container) { Component[] children = ((Container) component).getComponents(); for (Component child : children) { Component childResult = getFirstChildComponentOfType(child, childComponentType); if (childResult != null) { return childResult; } } } return null; } }