Java tutorial
//package com.java2s; /* * Copyright (C) 2009 Illya Yalovyy * Use is subject to license terms. */ import javax.swing.*; import java.awt.*; public class Main { /** * Finds the nearest RootPaneContainer of the provided Component. * Primarily, if a JPopupMenu (such as used by JMenus when they are visible) has no parent, * the search continues with the JPopupMenu's invoker instead. Fixes BSAF-77 * * @return a RootPaneContainer for the provided component * @param root the Component * * @author Eric Heumann * @since 1.9 */ public static RootPaneContainer findRootPaneContainer(Component root) { while (root != null) { if (root instanceof RootPaneContainer) { return (RootPaneContainer) root; } else if (root instanceof JPopupMenu && root.getParent() == null) { root = ((JPopupMenu) root).getInvoker(); } else { root = root.getParent(); } } return null; } }