Example usage for javax.swing JMenuBar getMenu

List of usage examples for javax.swing JMenuBar getMenu

Introduction

In this page you can find the example usage for javax.swing JMenuBar getMenu.

Prototype

public JMenu getMenu(int index) 

Source Link

Document

Returns the menu at the specified position in the menu bar.

Usage

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame("JRadioButtonMenuItem Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    bar.addNotify();// w  w  w  .  ja v a  2 s  .com
    JMenu menu = new JMenu("Options");
    menu.setMnemonic(KeyEvent.VK_O);

    ButtonGroup group = new ButtonGroup();

    JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem("A");
    group.add(menuItem);
    menu.add(menuItem);

    menuItem = new JRadioButtonMenuItem("B");
    group.add(menuItem);
    menu.add(menuItem);

    menuItem = new JRadioButtonMenuItem("C");
    group.add(menuItem);
    menu.add(menuItem);

    bar.add(menu);

    JMenu m = bar.getMenu(0);

    f.setJMenuBar(bar);
    f.setSize(300, 200);
    f.setVisible(true);
}

From source file:Main.java

/**
 * Finds a {@link JMenu} for the given {@link Action} within the given
 * {@link JMenuBar}./*from   w  w  w  .j ava2  s .  c  om*/
 *
 * @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;
}

From source file:Main.java

public final static JMenu getJMenu(JMenuBar menubar, String menuName) {
    JMenu theMenu = null;/* w ww  .j  a  v a2s  .  c o  m*/

    for (int i = 0; i < menubar.getMenuCount(); i++) {
        JMenu m = menubar.getMenu(i);
        //menubar may have components that are not JMenu.
        //Therefore, m may be NULL. See: JMenubar.getMenu()
        if (m != null && m.getName().equals(menuName)) {
            theMenu = m;
            i = menubar.getMenuCount();//break
        }
    }

    return theMenu;
}

From source file:Main.java

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;
    }/*  w w  w.  j  ava 2 s.c  o m*/
    return null;
}

From source file:Main.java

/**
 * @param newMenu//from w w w  .j  av  a2 s . co  m
 * @param menuBar
 * @param index
 * @return The same JMenuBar, for cascading.
 *         TODO See if the same thing can be done with Container.add( component, index )
 */
public static JMenuBar addMenuAt(JMenu newMenu, JMenuBar menuBar, int index) {

    ArrayList menuList = new ArrayList();
    for (int i = 0; i < menuBar.getMenuCount(); i++) {
        if (i == index) {
            menuList.add(newMenu);
        }
        menuList.add(menuBar.getMenu(i));
    }
    menuBar.removeAll();
    //        menuBar = new JMenuBar();
    for (int i = 0; i < menuList.size(); i++) {
        JMenu menu = (JMenu) menuList.get(i);
        menuBar.add(menu);
    }
    return menuBar;
}

From source file:Main.java

/**
 * Creates a copy of this menu bar, whose contents update automatically
 * whenever the original menu bar changes.
 *///from  w  ww  .  j  a v a2s.c  om
public static JMenuBar cloneMenuBar(final JMenuBar menubar) {
    if (menubar == null)
        return null;
    final JMenuBar jmb = new JMenuBar();
    final int count = menubar.getMenuCount();
    for (int i = 0; i < count; i++)
        jmb.add(cloneMenuItem(menubar.getMenu(i)));
    return jmb;
}

From source file:Main.java

/**
 * change background and foreground color for menu bar
 * @param aMenuBar menu bar//from   ww  w  .  j  av a2  s.c o  m
 * @param aBackgroundColor background color
 * @param aForegroundColor foreground color
 */
public final static void ChangeColor(JMenuBar aMenuBar, Color aBackgroundColor, Color aForegroundColor) {
    if (aBackgroundColor != null) {
        aMenuBar.setBackground(aBackgroundColor);
    }
    if (aForegroundColor != null) {
        aMenuBar.setForeground(aForegroundColor);
    }
    JMenu menu = null;
    for (int count = 0; count < aMenuBar.getMenuCount(); count++) {
        menu = aMenuBar.getMenu(count);
        ChangeColor(menu, aBackgroundColor, aForegroundColor);
    }
}

From source file:com.quattroresearch.antibody.plugin.PluginLoader.java

/**
 * Loads the menu plugins and inserts them into the menu.
 *//*from w w  w .  jav a2  s .c  om*/
public void loadMenuPlugins() {
    String menuPluginConfig = PreferencesService.getInstance().getApplicationPrefs().getString("plugins.menu");
    if (menuPluginConfig == null) {
        return;
    }
    String[] menuPlugins = menuPluginConfig.split(";");
    for (String singleConfig : menuPlugins) {
        try {

            Class<?> clazz = classLoader.findClass(singleConfig);

            if (clazz != null) {
                Constructor<?> constructor;

                constructor = clazz.getDeclaredConstructor(JFrame.class);

                AbstractEditorAction action = (AbstractEditorAction) constructor
                        .newInstance(UIService.getInstance().getMainFrame());

                boolean isAdded = false;
                JMenuBar menubar = UIService.getInstance().getMainFrame().getJMenuBar();
                for (int i = 0; i < menubar.getMenuCount(); i++) {
                    JMenu menu = menubar.getMenu(i);
                    if (menu.getText().equals(action.getMenuName())) {
                        menu.add(action);
                        isAdded = true;
                        break;
                    }
                }
                if (!isAdded) {
                    JMenu newMenu = new JMenu(action.getMenuName());
                    newMenu.add(action);
                    menubar.add(newMenu, menubar.getMenuCount() - 1);
                }
            }
        } catch (ClassNotFoundException exc) {
            System.err.println("Menu Plugin class was not found: " + exc.toString());
        } catch (Exception exc) {
            System.err.println(exc.toString());
        }
    }
}

From source file:com.jidesoft.spring.richclient.docking.JideApplicationLifecycleAdvisor.java

@Override
public void onPostStartup() {
    initializeRepaintManager();//from   w w  w.  j a va  2  s  .c om
    if (devOption == false) {
        JMenuBar menuBar = Application.instance().getActiveWindow().getControl().getJMenuBar();
        for (int i = 0; i < menuBar.getMenuCount(); i++) {
            //TODO:I18N
            if (menuBar.getMenu(i).getText().equals("Admin")) {
                menuBar.getMenu(i).setVisible(false);
            }
        }
    }
    JMenuBar menuBar = Application.instance().getActiveWindow().getControl().getJMenuBar();
    for (int i = 0; i < menuBar.getMenuCount(); i++) {
        // recent games
        //TODO: I18N
        if (menuBar.getMenu(i).getText().equals("Game")) {
            for (int j = 0; j < menuBar.getMenu(i).getItemCount(); j++) {
                if (menuBar.getMenu(i).getItem(j) != null && menuBar.getMenu(i).getItem(j).getText() != null
                        && menuBar.getMenu(i).getItem(j).getText().equals("Recent Games")) {
                    JMenu menu = (JMenu) menuBar.getMenu(i).getItem(j);
                    RecentGames rg = new RecentGames();
                    ArrayList<RecentGames.RecentGameInfo> rgis = rg.getRecentGameInfo();
                    for (RecentGameInfo rgi : rgis) {
                        final RecentGameInfo frgi = rgi;
                        JMenuItem mu = new JMenuItem();
                        mu.setText("Game " + String.valueOf(rgi.getNumber()));
                        mu.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                LoadGame loadGame = new LoadGame(frgi.getFile());
                                loadGame.execute();
                            }
                        });
                        menu.add(mu);
                    }
                }
            }
        }

        // user guide
        //TODO:I18N
        if (menuBar.getMenu(i).getText().equals("Help")) {
            try {
                final File f = new File("JOverseerUserGuide.pdf");
                if (f.exists()) {
                    //TODO:I18N
                    JMenuItem mi = new JMenuItem("User's Guide");
                    mi.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            try {
                                Runtime.getRuntime()
                                        .exec("rundll32 url.dll,FileProtocolHandler " + f.getAbsolutePath());
                            } catch (Exception exc) {
                                // do nothing
                            }
                        }
                    });
                    menuBar.getMenu(i).add(new JSeparator());
                    menuBar.getMenu(i).add(mi);
                }
            } catch (Exception exc) {
                // do nothing
            }
        }

    }

    if (JOverseerJIDEClient.cmdLineArgs != null && JOverseerJIDEClient.cmdLineArgs.length == 1
            && JOverseerJIDEClient.cmdLineArgs[0].endsWith(".jov")) {
        String fname = JOverseerJIDEClient.cmdLineArgs[0];
        File f = new File(fname);
        if (f.exists()) {
            LoadGame lg = new LoadGame(fname);
            lg.loadGame();
        }
    }
}