Java JMenuItem getTopLevelMenu(JMenuItem menuitem)

Here you can find the source of getTopLevelMenu(JMenuItem menuitem)

Description

Determines the top-level menu (the one sitting inside the JMenuBar) that is associated with a menu item.

License

Open Source License

Parameter

Parameter Description
menuitem the menu item to get the menu bar for

Return

the menu, null if not found

Declaration

public static JMenu getTopLevelMenu(JMenuItem menuitem) 

Method Source Code

//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;
    }
}

Related

  1. getForeground(final BasicMenuItemUI ui, final JMenuItem menuItem)
  2. getIconPlaceholderWidth(final JMenuItem menuItem, final boolean alignTextToMenuIcons)
  3. getInvoker(final JMenuItem menuItem)
  4. getJMenuItem(String name, JMenuBar jmenubar)
  5. getMenuItemParent(JMenuItem menuItem)
  6. layoutMenuItem(JMenuItem menuItem, FontMetrics fm, String text, FontMetrics fmAccel, String acceleratorText, Icon icon, Icon checkIcon, Icon arrowIcon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewRect, Rectangle iconRect, Rectangle textRect, Rectangle acceleratorRect, Rectangle checkIconRect, Rectangle arrowIconRect, int textIconGap, int menuItemGap)
  7. linkMenuItem(final JMenuItem master, final JMenuItem slave)
  8. pintaBarraMenu(Graphics g, JMenuItem menuItem, Color bgColor)
  9. recursiveUpdateOrInsertMenuItem(MenuElement menu, JMenuItem menuItem, boolean hideOnUpdate)