Java JComponent Container getFirstChildComponentOfType(Component component, Class childComponentType)

Here you can find the source of getFirstChildComponentOfType(Component component, Class childComponentType)

Description

Retrieves the first contained component of a certain type.

License

Open Source License

Parameter

Parameter Description
component the component to start from.
childComponentType the type of the component to look for.

Return

the first contained component of the looked for type or null if none.

Declaration

public static Component getFirstChildComponentOfType(Component component,
        Class<? extends JComponent> childComponentType) 

Method Source Code

//package com.java2s;
/*//w w w.  ja  v  a2  s  . c  o m
 * 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;
    }
}

Related

  1. getComponentByName(boolean enableDeepSearch, JComponent rootComponent, String name)
  2. getComponentsOfType(JComponent base, Class type)
  3. getComponentState(JComponent c)
  4. getDataFlavors(JComponent component)
  5. getDeepestEmptyComponentAt(JComponent parent, Point location)
  6. getGlassPane(JComponent comp)
  7. getGtkStyle(Object styleFactory, JComponent component, String regionName)
  8. getGtkStyle(Object styleFactory, JComponent component, String regionName)
  9. getInputHint(JComponent comp)