Here you can find the source of getTopLevelMenu(JMenuItem menuitem)
Parameter | Description |
---|---|
menuitem | the menu item to get the menu bar for |
public static JMenu getTopLevelMenu(JMenuItem menuitem)
//package com.java2s; /*//from w ww . j a v a 2 s . c o m * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import java.awt.Container; public class Main { /** * Determines the top-level menu (the one sitting inside the JMenuBar) that * is associated with a menu item. * * @param menuitem the menu item to get the menu bar for * @return the menu, null if not found */ public static JMenu getTopLevelMenu(JMenuItem menuitem) { JMenu result; Container cont; Container prev; result = null; cont = menuitem; prev = null; while (cont != null) { if (cont instanceof JPopupMenu) { result = (JMenu) prev; break; } prev = cont; cont = cont.getParent(); } return result; } /** * Tries to determine the parent this panel is part of. * * @param cont the container to get the parent for * @param parentClass the class of the parent to obtain * @return the parent if one exists or null if not */ public static Object getParent(Container cont, Class parentClass) { Container result; Container parent; result = null; parent = cont; while (parent != null) { if (parentClass.isInstance(parent)) { result = parent; break; } else { parent = parent.getParent(); } } return result; } }