Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Component;

import javax.swing.Action;

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

public class Main {
    /**
     * Finds a {@link JMenu} for the given {@link Action} within the given
     * {@link JMenuBar}.
     *
     * @param menuBar
     * @param action
     * @return
     */
    public static JMenu findMenu(JMenuBar menuBar, Action action) {
        for (int i = 0; i < menuBar.getMenuCount(); i++) {
            final JMenu found = findMenu(menuBar.getMenu(i), action);
            if (found != null)
                return found;
        }
        return null;
    }

    /**
     * Finds a {@link JMenu} for the given {@link Action} within the given
     * menu.
     *
     * @param menu
     * @param action
     * @return
     */
    public static JMenu findMenu(JMenu menu, Action action) {
        if (action.equals(menu.getAction())) {
            return menu;
        }
        for (Component comp : menu.getMenuComponents()) {
            if (comp instanceof JMenu) {
                if (((JMenu) comp).getAction().equals(action)) {
                    return (JMenu) comp;
                }
                return findMenu((JMenu) comp, action);
            }
        }
        return null;
    }
}