Here you can find the source of getWindow(final Object source)
public static Window getWindow(final Object source)
//package com.java2s; //License from project: Apache License import java.awt.Component; import java.awt.MenuContainer; import java.awt.MenuItem; import java.awt.Window; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import javax.swing.SwingUtilities; public class Main { public static Window getWindow(final Object source) { if (source instanceof JMenuItem) { final JMenuItem menuItem = (JMenuItem) source; MenuContainer menuContainer = menuItem.getParent(); while (menuContainer != null && !(menuContainer instanceof JPopupMenu)) { if (menuContainer instanceof MenuItem) { menuContainer = ((MenuItem) menuContainer).getParent(); } else { menuContainer = null; }/*from www .ja v a2 s .co m*/ } if (menuContainer != null) { final JPopupMenu menu = (JPopupMenu) menuContainer; final Component invoker = menu.getInvoker(); return SwingUtilities.getWindowAncestor(invoker); } } else if (source instanceof Component) { return SwingUtilities.getWindowAncestor((Component) source); } return null; } }