Java Swing Menu Item findMenuItem(Container container, String[] path)

Here you can find the source of findMenuItem(Container container, String[] path)

Description

Traverse a container hierarchy and returns the JMenuItem with the given path

License

Open Source License

Declaration

static JMenuItem findMenuItem(Container container, String[] path) 

Method Source Code


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

Related

  1. createMenuItem(String text, boolean visible)
  2. createMenuItem(String text, Icon icon, String toolTip, ActionListener... listeners)
  3. createMenuItem(T source, int mnemonic, String description, E action)
  4. createRadioButtonMenuItem(final String text, final boolean selected)
  5. faqMenuItem()
  6. getExitMenuItem()
  7. getMenuItem(Container container, Action action)
  8. getMenuItem(Container owner, String text)
  9. getMenuItemBorder()