Java Swing Menu findMenuElement(final MenuElement root, final String text)

Here you can find the source of findMenuElement(final MenuElement root, final String text)

Description

find Menu Element

License

Open Source License

Declaration

public static MenuElement findMenuElement(final MenuElement root,
            final String text) 

Method Source Code

//package com.java2s;
/*// w  w w .java2 s . com
 * JTA utilities
 * 
 * (c) Walter Di Carlo 2010. All Rights Reserved.
 *
 * --LICENSE NOTICE--
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * --LICENSE NOTICE--
 *
 */

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.MenuElement;

public class Main {
    public static MenuElement findMenuElement(final MenuElement root,
            final String text) {
        for (MenuElement item : root.getSubElements()) {
            if (item instanceof JMenu == true) {
                JMenu mItem = (JMenu) item;
                if (mItem.getSubElements().length > 0) {
                    MenuElement elem = findMenuElement(mItem, text);
                    if (elem != null)
                        return elem;
                }
                continue;
            }
            if (item instanceof JPopupMenu == true) {
                JPopupMenu mItem = (JPopupMenu) item;
                if (mItem.getSubElements().length > 0) {
                    MenuElement elem = findMenuElement(mItem, text);
                    if (elem != null)
                        return elem;
                }
                continue;
            }

            if (item instanceof JMenuItem == false)
                continue;
            JMenuItem mItem = (JMenuItem) item;
            if (mItem.getText().equals(text) == true)
                return mItem;
        }
        return null;
    }
}

Related

  1. createMenu(String name, Object[] item, int[] accelerator, ActionListener listener)
  2. createMenu(String text)
  3. createMenuKeyMask(final int aKeyStroke, final int... aMasks)
  4. enableMenu(JTextComponent text)
  5. findMenuBar(Container container)
  6. fixIconTextGap(JComponent menu)
  7. getAllMenuSubElements( MenuElement root)
  8. getMenuFontHeight()
  9. getMenuKeyStroke(String stroke)