Java tutorial
//package com.java2s; /*************************************************************************** * Copyright 2001-2007 The eXo Platform SARL All rights reserved. * * Please look at license.txt in info directory for more license detail. * **************************************************************************/ import java.awt.Container; import javax.swing.JComponent; public class Main { static public <T> T getAncestorOfType(JComponent component, Class<T> type) { Container ancestor = component.getParent(); while (ancestor != null) { System.out.println("Check parent : " + ancestor); if (type.isInstance(ancestor)) return (T) ancestor; ancestor = ancestor.getParent(); } return null; } }