Java tutorial
//package com.java2s; import java.awt.Component; public class Main { /** * Get specified parent. * @param <T> * @param component * @param clazz * @return */ public static <T> T getParent(Component component, Class<T> clazz) { if (component == null) { return null; } if (clazz.isInstance(component)) { return (T) component; } return getParent(component.getParent(), clazz); } }