Java tutorial
//package com.java2s; /* * OpenBench LogicSniffer / SUMP project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program 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 * General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * * * Copyright (C) 2010-2011 - J.W. Janssen, http://www.lxtreme.nl */ import java.awt.*; public class Main { /** * Convenience method for searching above the given component in the component * hierarchy and returns the first object of the given type it finds, or * <code>null</code> if no such parent was found. * <p> * The reason this method exists is for tidyness of the calling code, as no * longer a explicit cast is needed. * * @param aType * the type of the parent to find, cannot be <code>null</code>; * @param aComponent * the component to search in the hierarchy, cannot be * <code>null</code>. * @return the requested ancestor, or <code>null</code> if not found. * @see SwingUtilities#getAncestorOfClass(Class, Component) */ @SuppressWarnings("unchecked") public static <T> T getAncestorOfClass(final Class<T> aType, final Component aComponent) { if ((aComponent == null) || (aType == null)) { return null; } Container parent = aComponent.getParent(); while ((parent != null) && !(aType.isInstance(parent))) { parent = parent.getParent(); } return (T) parent; } }