Example usage for javax.swing JMenu getMenuComponentCount

List of usage examples for javax.swing JMenu getMenuComponentCount

Introduction

In this page you can find the example usage for javax.swing JMenu getMenuComponentCount.

Prototype

@BeanProperty(bound = false)
public int getMenuComponentCount() 

Source Link

Document

Returns the number of components on the menu.

Usage

From source file:Main.java

/** Convenience method that recursively enables every JMenu and JMenuItem inside "menu".
 * @param menu - the menu to start the recursive search
 *///from  w w w .  j  a va2 s.  co m
public static void enableAll(JMenu menu) {
    for (int i = 0; i < menu.getMenuComponentCount(); i++) {
        Component x = menu.getMenuComponent(i);
        if (x instanceof JMenuItem)
            ((JMenuItem) x).setEnabled(true);
        else if (x instanceof JMenu)
            enableAll((JMenu) x);
    }
}

From source file:Main.java

public final static int countCheckBoxMenuItem(JMenu menu) {
    int count = 0;
    for (int i = 0; i < menu.getMenuComponentCount(); i++) {
        Component c = menu.getMenuComponent(i);
        if (c instanceof JCheckBoxMenuItem) {
            count++;//from  w ww.j  a va  2s. c  om
        }
    }
    return count;
}

From source file:Main.java

/**add un menbu al menu padre en la posicion correspondiente, si piPosicion es negativo lo inserta por el final*/
public static void addMenu(JMenu poMenuPadre, JMenuItem poElemento, int piPosicion) {
    int liLongitud = poMenuPadre.getMenuComponentCount();
    int liPosSeparador = -1;
    if (piPosicion < 0) {
        poMenuPadre.insert(poElemento, (liPosSeparador != -1 ? liPosSeparador : liLongitud) + piPosicion);
    } else {//from  w ww .  ja  va 2 s. c o  m
        poMenuPadre.insert(poElemento, (liPosSeparador != -1 ? liPosSeparador : 0) + piPosicion);
    }
}

From source file:Main.java

public final static int getMenuItemIndex(JMenu menu, String menuItemText) {
    int theIdx = -1;

    for (int i = 0; i < menu.getMenuComponentCount(); i++) {
        Component c = menu.getMenuComponent(i);
        if (c instanceof JMenuItem) {
            JMenuItem mi = (JMenuItem) c;
            if (mi.getText().equals(menuItemText)) {
                theIdx = i;//from   www.  ja  v  a  2 s .  c  o  m
                i = menu.getMenuComponentCount();// break
            }
        }
    }

    return theIdx;
}

From source file:MainClass.java

private void applyOrientation(Component c, ComponentOrientation o) {
    c.setComponentOrientation(o);//from  ww  w  .j av a  2  s .c  o  m

    if (c instanceof JMenu) {
        JMenu menu = (JMenu) c;
        int ncomponents = menu.getMenuComponentCount();
        for (int i = 0; i < ncomponents; ++i) {
            applyOrientation(menu.getMenuComponent(i), o);
        }
    } else if (c instanceof Container) {
        Container container = (Container) c;
        int ncomponents = container.getComponentCount();
        for (int i = 0; i < ncomponents; ++i) {
            applyOrientation(container.getComponent(i), o);
        }
    }
}

From source file:de.tud.kom.p2psim.impl.skynet.visualization.SkyNetVisualization.java

private void displayAllMetricsOfAGroup(JMenu menu) {
    JCheckBoxMenuItem item = null;
    String name = null;//from  www  .  j av  a 2 s  .c o  m
    for (int i = 0; i < menu.getMenuComponentCount(); i++) {
        if (menu.getMenuComponent(i) instanceof JCheckBoxMenuItem) {
            item = (JCheckBoxMenuItem) menu.getMenuComponent(i);
            name = item.getActionCommand();
            if (!item.isSelected()) {
                item.setSelected(true);
                createPlotInWindow(name);
            }
        }
    }
}

From source file:de.tud.kom.p2psim.impl.skynet.visualization.SkyNetVisualization.java

private void removeAllMetricsOfAGroup(JMenu menu) {
    JCheckBoxMenuItem item = null;
    String name = null;/*w  ww .  j  a va 2s. com*/
    for (int i = 0; i < menu.getMenuComponentCount(); i++) {
        if (menu.getMenuComponent(i) instanceof JCheckBoxMenuItem) {
            item = (JCheckBoxMenuItem) menu.getMenuComponent(i);
            name = item.getActionCommand();
            if (item.isSelected()) {
                item.setSelected(false);
                deletePlotInWindow(name);
            }
        }
    }
}

From source file:EditorPaneExample20.java

public static JMenu buildMenu(String name, MenuSpec[] menuSpecs, Hashtable actions) {
    int count = menuSpecs.length;

    JMenu menu = new JMenu(name);
    for (int i = 0; i < count; i++) {
        MenuSpec spec = menuSpecs[i];//from w  ww .j a va2  s  .  c o m
        if (spec.isSubMenu()) {
            // Recurse to handle a sub menu
            JMenu subMenu = buildMenu(spec.getName(), spec.getSubMenus(), actions);
            if (subMenu != null) {
                menu.add(subMenu);
            }
        } else if (spec.isAction()) {
            // It's an Action - add it directly to the menu
            menu.add(spec.getAction());
        } else {
            // It's an action name - add it if possible
            String actionName = spec.getActionName();
            Action targetAction = (Action) actions.get(actionName);

            // Create the menu item
            JMenuItem menuItem = menu.add(spec.getName());
            if (targetAction != null) {
                // The editor kit knows the action
                menuItem.addActionListener(targetAction);
            } else {
                // Action not known - disable the menu item
                menuItem.setEnabled(false);
            }
        }
    }

    // Return null if nothing was added to the menu.
    if (menu.getMenuComponentCount() == 0) {
        menu = null;
    }

    return menu;
}

From source file:ee.ioc.cs.vsle.editor.Editor.java

/**
 * Build menu./*from www  .j a va 2s. c  o m*/
 */
public void makeMenu() {
    JMenuItem menuItem;

    JMenu menu;
    JMenu submenu;

    menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    menu = new JMenu(Menu.MENU_FILE);
    menu.setMnemonic(KeyEvent.VK_F);
    menuItem = new JMenuItem(Menu.NEW_SCHEME, KeyEvent.VK_N);
    menuItem.addActionListener(getActionListener());
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
    menu.add(menuItem);
    menu.addSeparator();
    menuItem = new JMenuItem(Menu.LOAD_SCHEME, KeyEvent.VK_O);
    menuItem.addActionListener(getActionListener());
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
    menu.add(menuItem);
    menuItem = new JMenuItem(Menu.RELOAD_SCHEME, KeyEvent.VK_R);
    menuItem.addActionListener(getActionListener());
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
    menu.add(menuItem);
    menu.addSeparator();
    menuItem = new JMenuItem(Menu.SAVE_SCHEME, KeyEvent.VK_S);
    menuItem.addActionListener(getActionListener());
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
    menu.add(menuItem);
    menuItem = new JMenuItem(Menu.SAVE_SCHEME_AS);
    menuItem.addActionListener(getActionListener());
    menuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
    menu.add(menuItem);
    menu.addSeparator();
    menuItem = new JMenuItem(Menu.DELETE_SCHEME, KeyEvent.VK_D);
    menuItem.addActionListener(getActionListener());
    menu.add(menuItem);

    submenu = new JMenu(Menu.EXPORT_MENU);
    menu.add(submenu);
    //submenu.setMnemonic( KeyEvent.VK_E );

    SchemeExporter.makeSchemeExportMenu(submenu, getActionListener());

    // Export window graphics
    submenu.add(GraphicsExporter.getExportMenu());

    menu.addSeparator();
    menuItem = new JMenuItem(Menu.PRINT, KeyEvent.VK_P);
    menuItem.addActionListener(getActionListener());
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
    menu.add(menuItem);
    menu.addSeparator();
    menuItem = new JMenuItem(Menu.EXIT, KeyEvent.VK_X);
    menuItem.addActionListener(getActionListener());
    menu.add(menuItem);
    menuBar.add(menu);
    menu = new JMenu(Menu.MENU_EDIT);
    menu.setMnemonic(KeyEvent.VK_E);

    menu.add(undoAction);
    menu.add(redoAction);
    menu.add(cloneAction);

    menuItem = new JMenuItem(Menu.SCHEME_FIND, KeyEvent.VK_F);
    menuItem.addActionListener(getActionListener());
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
    menu.add(menuItem);

    menuItem = new JMenuItem(Menu.SELECT_ALL, KeyEvent.VK_A);
    menuItem.addActionListener(getActionListener());
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
    menu.add(menuItem);
    menuItem = new JMenuItem(Menu.CLEAR_ALL, KeyEvent.VK_C);
    menuItem.addActionListener(getActionListener());
    menu.add(menuItem);

    final JCheckBoxMenuItem painterEnabled = new JCheckBoxMenuItem(Menu.CLASSPAINTER, true);
    painterEnabled.addActionListener(getActionListener());
    menu.add(painterEnabled);

    menu.getPopupMenu().addPopupMenuListener(new PopupMenuListener() {

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
            // ignore
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            // ignore
        }

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            Canvas canvas = Editor.getInstance().getCurrentCanvas();
            if (canvas == null || !canvas.getPackage().hasPainters()) {
                painterEnabled.setVisible(false);
            } else {
                painterEnabled.setVisible(true);
                painterEnabled.setSelected(canvas.isEnableClassPainter());
            }
        }

    });

    menuBar.add(menu);

    menu = new JMenu(Menu.MENU_VIEW);
    menu.setMnemonic(KeyEvent.VK_V);
    gridCheckBox = new JCheckBoxMenuItem(Menu.GRID, RuntimeProperties.isShowGrid());
    gridCheckBox.setMnemonic('G');
    gridCheckBox.addActionListener(getActionListener());
    menu.add(gridCheckBox);

    ctrlCheckBox = new JCheckBoxMenuItem(Menu.CONTROL_PANEL, RuntimeProperties.isShowControls());
    ctrlCheckBox.setMnemonic('C');
    ctrlCheckBox.addActionListener(getActionListener());
    menu.add(ctrlCheckBox);

    showPortCheckBox = new JCheckBoxMenuItem(Menu.SHOW_PORTS, true);
    showPortCheckBox.addActionListener(getActionListener());
    menu.add(showPortCheckBox);

    showObjectNamesCheckBox = new JCheckBoxMenuItem(Menu.SHOW_NAMES, false);
    showObjectNamesCheckBox.addActionListener(getActionListener());
    menu.add(showObjectNamesCheckBox);

    //sync View with current canvas
    menu.getPopupMenu().addPopupMenuListener(new PopupMenuListener() {

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            Canvas canvas;

            if ((canvas = getCurrentCanvas()) == null)
                return;

            gridCheckBox.setSelected(canvas.isGridVisible());
            ctrlCheckBox.setSelected(canvas.isCtrlPanelVisible());
            showPortCheckBox.setSelected(canvas.isDrawPorts());
            showObjectNamesCheckBox.setSelected(canvas.isShowObjectNames());
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            // ignore
        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
            // ignore
        }

    });

    menuBar.add(menu);

    menu = new JMenu(Menu.MENU_PACKAGE);
    menu.setMnemonic(KeyEvent.VK_P);
    menuItem = new JMenuItem(Menu.LOAD, KeyEvent.VK_L);
    menuItem.addActionListener(getActionListener());
    menu.add(menuItem);
    menuItem = new JMenuItem(Menu.RELOAD, KeyEvent.VK_R);
    menuItem.addActionListener(getActionListener());
    menu.add(menuItem);
    menuItem = new JMenuItem(Menu.INFO, KeyEvent.VK_I);
    menuItem.addActionListener(getActionListener());
    menu.add(menuItem);
    if (Desktop.isDesktopSupported()) {
        menuItem = new JMenuItem(Menu.BROWSE_PACKAGE, KeyEvent.VK_B);
        menuItem.addActionListener(getActionListener());
        menu.add(menuItem);
    }
    menuItem = new JMenuItem(Menu.CLOSE, KeyEvent.VK_C);
    menuItem.addActionListener(getActionListener());
    menu.add(menuItem);
    menuItem = new JMenuItem(Menu.CLOSE_ALL);
    menuItem.addActionListener(getActionListener());
    menu.add(menuItem);
    menuBar.add(menu);
    menu.add(new JSeparator());
    final JMenu submenuRecent = new JMenu(Menu.RECENT);
    submenuRecent.getPopupMenu().addPopupMenuListener(new PopupMenuListener() {

        final JMenuItem empty = new JMenuItem("Empty");

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {

            makeRecentSubMenu(submenuRecent);

            if (submenuRecent.getMenuComponentCount() == 0) {

                submenuRecent.add(empty);
                empty.setEnabled(false);

            } else {
                if (!((submenuRecent.getMenuComponentCount() == 1)
                        && (submenuRecent.getPopupMenu().getComponentIndex(empty) >= -1))) {
                    submenuRecent.remove(empty);
                }
            }

        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            // ignore
        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
            // ignore
        }

    });
    menu.add(submenuRecent);
    final JMenu menuScheme = new JMenu(Menu.MENU_SCHEME);
    menuScheme.setMnemonic(KeyEvent.VK_S);
    makeSchemeMenu(menuScheme);

    menuScheme.getPopupMenu().addPopupMenuListener(new PopupMenuListener() {

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {

            makeSchemeMenu(menuScheme);

        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            // ignore
        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
            // ignore
        }

    });

    /*
     * menuItem = new JMenuItem("Planner");
     * menuItem.addActionListener(aListener); menuScheme.add(menuItem);
     * menuItem = new JMenuItem("Plan, compile, run");
     * menuItem.setActionCommand("Run");
     * menuItem.addActionListener(aListener); menuScheme.add(menuItem);
     */
    // menuScheme.setMnemonic(KeyEvent.VK_A);
    menuBar.add(menuScheme);
    menu = new JMenu(Menu.MENU_OPTIONS);
    menu.setMnemonic(KeyEvent.VK_O);

    menuItem = new JMenuItem(Menu.SETTINGS, KeyEvent.VK_S);
    menuItem.addActionListener(getActionListener());
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_J, ActionEvent.CTRL_MASK));
    menu.add(menuItem);

    menuItem = new JMenuItem(Menu.FONTS);
    menuItem.addActionListener(getActionListener());
    menu.add(menuItem);

    menuItem = new JMenuItem(Menu.SAVE_SETTINGS);
    menuItem.addActionListener(getActionListener());
    menu.add(menuItem);

    submenu = new JMenu(Menu.MENU_LAF);
    submenu.setMnemonic(KeyEvent.VK_L);
    Look.getInstance().createMenuItems(submenu);
    menu.add(submenu);
    menuBar.add(menu);

    makeToolsMenu(menuBar);

    menu = new JMenu(Menu.MENU_HELP);
    menu.setMnemonic(KeyEvent.VK_H);
    menuBar.add(menu);
    menuItem = new JMenuItem(Menu.DOCS, KeyEvent.VK_D);
    menuItem.addActionListener(getActionListener());
    menu.add(menuItem);
}