Here you can find the source of findMenuItem(Container container, String[] path)
static JMenuItem findMenuItem(Container container, String[] path)
//package com.java2s; // it under the terms of the GNU General Public License as published by import java.awt.Component; import java.awt.Container; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.MenuElement; public class Main { /**//from ww w.j a v a 2 s .c om * Traverse a container hierarchy and returns the JMenuItem with * the given text * */ static JMenuItem findMenuItem(MenuElement container, String text) { MenuElement[] components = container.getSubElements(); for (MenuElement component : components) { if (component instanceof JMenuItem) { JMenuItem button = (JMenuItem) component; if (button.getText().equals(text)) { return button; } } else { JMenuItem button = findMenuItem(component, text); if (button != null) { return button; } } } return null; } /** * Traverse a container hierarchy and returns the JMenuItem with * the given path * */ static JMenuItem findMenuItem(Container container, String[] path) { if (path.length == 0) return null; MenuElement currentItem = findMenuBar(container); if (currentItem == null) return null; for (int i = 0; i < path.length; i++) { currentItem = findMenuItem(currentItem, path[i]); if (currentItem == null) return null; } return (JMenuItem) currentItem; } /** * Traverse a container hierarchy and returns the first JMenuBar * it finds. * */ static JMenuBar findMenuBar(Container container) { Component[] components = container.getComponents(); for (Component component : components) { if (component instanceof JMenuBar) { return (JMenuBar) component; } else if (component instanceof Container) { JMenuBar jmb = findMenuBar((Container) component); if (jmb != null) { return jmb; } } } return null; } }