Java JMenuBar getMenuItem(JMenuBar menu, String text)

Here you can find the source of getMenuItem(JMenuBar menu, String text)

Description

get Menu Item

License

Open Source License

Declaration

public static JMenuItem getMenuItem(JMenuBar menu, String text) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;
import java.awt.Container;

import java.util.ArrayList;

import java.util.List;
import javax.swing.AbstractButton;

import javax.swing.JComponent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class Main {
    public static JMenuItem getMenuItem(JMenuBar menu, String text) {
        for (int i = 0; i < menu.getMenuCount(); i++) {
            JMenuItem m = getMenuItem(menu.getMenu(i), text);
            if (m != null)
                return m;
        }/*from   w  w w  . j ava  2 s. c  o m*/
        return null;
    }

    private static JMenuItem getMenuItem(JMenu menu, String text) {
        for (Component c : menu.getMenuComponents()) {
            if (c instanceof JMenu) {
                JMenuItem m = getMenuItem((JMenu) c, text);
                if (m != null)
                    return m;
            } else if (c instanceof JMenuItem && ((JMenuItem) c).getText().equals(text)) {
                return (JMenuItem) c;
            }
        }
        return null;
    }

    public static JMenuItem getMenuItem(Container owner, String text) {
        return (JMenuItem) getAbstractButton(owner, text);
    }

    private static AbstractButton getAbstractButton(Container owner, String text) {
        for (Component c : owner.getComponents()) {
            if (c instanceof AbstractButton && ((AbstractButton) c).getText() != null
                    && ((AbstractButton) c).getText().equals(text))
                return (AbstractButton) c;
            else if (c instanceof JComponent) {
                AbstractButton b = getAbstractButton((JComponent) c, text);
                if (b != null)
                    return b;
            }
        }
        return null;
    }

    public static List<JComponent> getComponents(Container owner, Class<?> clazz) {
        return getComponents(owner, clazz, false);
    }

    public static List<JComponent> getComponents(Container owner, Class<?> clazz, boolean onlyVisible) {
        List<JComponent> list = new ArrayList<JComponent>();
        for (Component c : owner.getComponents()) {
            if (clazz.isInstance(c) && (!onlyVisible || c.isShowing()))
                list.add((JComponent) c);
            else if (c instanceof JComponent) {
                for (JComponent b : getComponents((JComponent) c, clazz, onlyVisible))
                    list.add(b);
            }
        }
        return list;
    }
}

Related

  1. findMenuComponent( JMenuBar menuBar, String menuName, String menuComponentName, Class componentClass)
  2. findMenuPosition(JMenuBar menuBar, String name)
  3. getJMenu(JMenuBar menubar, String menuName)
  4. getJMenuByName(JMenuBar bar, String name)
  5. getMenu(JMenuBar loMenuBar, String psComando)
  6. launch(String title, JComponent component, JMenuBar menuBar)
  7. menu(String text, char mnemonic, JMenuBar menuBar)
  8. pushMenu(JMenuBar jmenubar, String[][] menus)
  9. selectMenuItem(JMenuBar menuBar, Action action, boolean selected)