Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.Container; import java.awt.Window; public class Main { public static Window findWindow(Component comp) { return (Window) findContainer(comp, Window.class); } public static Container findContainer(Component thisComp, Class<? extends Container> containerClass) { if (thisComp == null) { return null; } Container parent = thisComp.getParent(); do { parent = parent.getParent(); if (parent != null && containerClass.isAssignableFrom(parent.getClass())) { return parent; } } while (parent != null); return null; } }