Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.Window; public class Main { /** * Close the parent window. * @see Window#dispose() * @param component */ public static void disposeWindow(Component component) { if (component == null) { return; } Window window = getParent(component, Window.class); if (window == null) { return; } window.dispose(); } /** * 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); } }