Here you can find the source of getAncestorOfType(JComponent component, Class
static public <T> T getAncestorOfType(JComponent component, Class<T> type)
//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(); }/*from w w w . jav a 2 s . co m*/ return null; } }