Java examples for Swing:JMenu
Finds the top level parent JPopupMenu of the given component, it it is contained in the component tree of one.
/*/* ww w .j a v a 2 s . c o m*/ * UIUtil.java of project jchart2d, utility class for UI / Layout operations. * Copyright (C) 2004 - 2011 Achim Westermann. * * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA* If you modify or optimize the code in a useful way please let * me know. Achim.Westermann@gmx.de */ import java.awt.Component; import java.awt.Frame; import java.awt.Point; import java.awt.Window; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JPopupMenu; public class Main{ /** * Finds the top level parent <code>JPopupMenu</code> of the given component, * it it is contained in the component tree of one. * <p> * * <code>JPopupMenu</code> trees may contain many sub menu instances. * <p> * * @param component * a potential sub component of a popup menu. * * @return the popup menu of the given component or null. */ public static JPopupMenu findTopLevelPopupMenu(final Component component) { JPopupMenu result = null; Component comp = component; if (component instanceof JPopupMenu) { result = ((JPopupMenu) component); comp = result.getInvoker(); } else { comp = component.getParent(); } // try to search for a higher popup: if (comp != null) { JPopupMenu higher = UIUtil.findTopLevelPopupMenu(comp); if (higher != null) { result = higher; } } return result; } }