Example usage for javax.swing JMenuItem setMnemonic

List of usage examples for javax.swing JMenuItem setMnemonic

Introduction

In this page you can find the example usage for javax.swing JMenuItem setMnemonic.

Prototype

@BeanProperty(visualUpdate = true, description = "the keyboard character mnemonic")
public void setMnemonic(int mnemonic) 

Source Link

Document

Sets the keyboard mnemonic on the current model.

Usage

From source file:org.sbml.bargraph.MainWindow.java

/**
 * Set up menus when running under Windows.
 *///  w w  w.  j  a v a  2  s  . c  o m
public void registerForEvents() {
    Log.note("Setting up the Windows menus.");

    JMenuItem fileExitItem = new JMenuItem();
    fileExitItem
            .setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, shortcutKeyMask));
    fileExitItem.setMnemonic(java.awt.event.KeyEvent.VK_C);
    fileExitItem.setText("Exit");
    fileExitItem.setToolTipText("Exit application");
    fileExitItem.addActionListener(new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            quit();
        }
    });

    fileMenu.addSeparator();
    fileMenu.add(fileExitItem);

    JMenu helpMenu = new JMenu();
    helpMenu.setText("Help");

    JMenuItem aboutMenuItem = new JMenuItem();
    aboutMenuItem.setText("About " + Config.APP_NAME);
    aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            about();
        }
    });

    helpMenu.add(aboutMenuItem);
    menuBar.add(helpMenu);
}

From source file:cl.almejo.vsim.gui.SimWindow.java

private JMenuItem newMenuItem(WindowAction action, char mnemonic) {
    JMenuItem menuItem = new JMenuItem(action);
    menuItem.setMnemonic(mnemonic);
    return menuItem;
}

From source file:adams.gui.tools.FileMonitorPanel.java

/**
 * Creates a menu bar (singleton per panel object). Can be used in frames.
 *
 * @return      the menu bar// w w  w.ja  v a  2 s  .  com
 */
@Override
public JMenuBar getMenuBar() {
    JMenuBar result;
    JMenu menu;
    JMenu submenu;
    JMenuItem menuitem;

    if (m_MenuBar == null) {
        result = new JMenuBar();

        // File
        menu = new JMenu("File");
        result.add(menu);
        menu.setMnemonic('F');
        menu.addChangeListener((ChangeEvent e) -> updateMenu());

        menuitem = new JMenuItem("Open...", GUIHelper.getIcon("open.gif"));
        menuitem.setMnemonic('O');
        menuitem.setAccelerator(GUIHelper.getKeyStroke("ctrl pressed O"));
        menuitem.addActionListener((ActionEvent e) -> open());
        menu.add(menuitem);
        m_MenuItemOpen = menuitem;

        // File/Recent files
        submenu = new JMenu("Open recent");
        menu.add(submenu);
        m_RecentFilesHandler = new RecentFilesHandler<>(SESSION_FILE, 5, submenu);
        m_RecentFilesHandler.addRecentItemListener(new RecentItemListener<JMenu, File>() {
            public void recentItemAdded(RecentItemEvent<JMenu, File> e) {
                // ignored
            }

            public void recentItemSelected(RecentItemEvent<JMenu, File> e) {
                open(e.getItem());
            }
        });
        m_MenuItemOpenRecent = submenu;

        menu.addSeparator();

        menuitem = new JMenuItem("Close", GUIHelper.getIcon("exit.png"));
        menuitem.setMnemonic('C');
        menuitem.setAccelerator(GUIHelper.getKeyStroke("ctrl pressed Q"));
        menuitem.addActionListener((ActionEvent e) -> close());
        menu.add(menuitem);
        m_MenuItemClose = menuitem;

        // update menu
        m_MenuBar = result;
        updateMenu();
    } else {
        result = m_MenuBar;
    }

    return result;
}

From source file:com.googlecode.bpmn_simulator.gui.BPMNSimulatorFrame.java

private JMenu createMenuHelp() {
    final JMenu menuHelp = new JMenu(Messages.getString("Menu.help")); //$NON-NLS-1$

    final JMenuItem menuHelpAbout = new JMenuItem(Messages.getString("Menu.about")); //$NON-NLS-1$
    menuHelpAbout.setMnemonic(KeyEvent.VK_A);
    menuHelpAbout.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.ALT_MASK));
    menuHelpAbout.addActionListener(new ActionListener() {
        @Override/*from  w  w  w. ja  v a  2s  .c o m*/
        public void actionPerformed(final ActionEvent e) {
            showAboutDialog();
        }
    });
    menuHelp.add(menuHelpAbout);

    return menuHelp;
}

From source file:de.codesourcery.jasm16.ide.ui.MenuManager.java

protected JMenuBar createMenuBar() {
    final List<MenuEntry> copy;
    synchronized (entriesList) {
        copy = new ArrayList<MenuEntry>(entriesList);
    }//from w w w . j  av a2 s . com

    // collect distinct parent paths
    final List<MenuPath> paths = new ArrayList<MenuPath>();
    for (MenuEntry e : copy) {
        if (!e.isVisible()) {
            continue;
        }
        final MenuPath parentPath = e.getPath().getParentPath();
        if (parentPath == null) {
            continue;
        }
        for (MenuPath p : parentPath.getAllPaths()) {
            if (!paths.contains(p)) {
                paths.add(p);
            }
        }
    }

    // sort paths ascending by length
    Collections.sort(paths, new Comparator<MenuPath>() {

        @Override
        public int compare(MenuPath o1, MenuPath o2) {
            final int len1 = o1.toString().length();
            final int len2 = o2.toString().length();
            if (len1 < len2) {
                return -1;
            } else if (len1 > len2) {
                return 1;
            }
            return 0;
        }
    });

    /*
     * - a
     *   |
     *   +-- b
     *       |
     *       + c  
     */

    // create menu for each path
    final Map<MenuPath, JMenu> menuesByPath = new HashMap<MenuPath, JMenu>();
    for (MenuPath path : paths) {
        final JMenu menu = new JMenu(path.getLastPathComponent());
        menuesByPath.put(path, menu);
        JMenu parentMenu = menuesByPath.get(path.getParentPath());
        if (parentMenu != null) {
            parentMenu.add(menu);
        }
    }

    // setup menu bar

    //Where the GUI is created:
    final JMenuBar menuBar = new JMenuBar();

    final Set<MenuPath> topLevelMenues = new HashSet<MenuPath>();

    for (final MenuEntry e : copy) {

        if (!e.isVisible()) {
            continue;
        }

        final MenuPath parentPath = e.getPath().getParentPath();
        final JMenu menu = menuesByPath.get(parentPath);
        if (menu == null) {
            throw new RuntimeException("Internal error, failed to create menu for path: " + e.getPath());
        }

        // register top-level menues
        if (parentPath.length() == 1) {
            if (!topLevelMenues.contains(parentPath)) {
                menuBar.add(menu);
                topLevelMenues.add(parentPath);
            }
        }

        final Action action;
        action = new AbstractAction(e.getLabel()) {

            @Override
            public void actionPerformed(ActionEvent event) {
                e.onClick();
            }

            @Override
            public boolean isEnabled() {
                return e.isEnabled();
            }

        };

        final JMenuItem item = new JMenuItem(action) {

            @Override
            public boolean isEnabled() {
                return action.isEnabled();
            }
        };

        if (e.hasMnemonic()) {
            item.setMnemonic(e.getMnemonic());
        }
        e.setMenuItem(item);
        menu.add(item);
    }

    return menuBar;
}

From source file:com.googlecode.bpmn_simulator.gui.BPMNSimulatorFrame.java

@Override
protected JMenu createWindowMenu() {
    final JMenu menuWindow = new JMenu(Messages.getString("Menu.windows")); //$NON-NLS-1$

    final JMenuItem menuWindowElements = new JMenuItem(Messages.getString("Menu.elements")); //$NON-NLS-1$
    menuWindowElements.setMnemonic(KeyEvent.VK_E);
    menuWindowElements.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.ALT_MASK));
    menuWindowElements.addActionListener(new ActionListener() {
        @Override//  w w  w  .  j  a v a2s .c  o  m
        public void actionPerformed(final ActionEvent e) {
            showElementsFrame();
        }
    });
    menuWindow.add(menuWindowElements);

    final JMenuItem menuWindowInstances = new JMenuItem(Messages.getString("Menu.instances")); //$NON-NLS-1$
    menuWindowInstances.setMnemonic(KeyEvent.VK_I);
    menuWindowInstances.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.ALT_MASK));
    menuWindowInstances.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            showInstancesFrame();
        }
    });
    menuWindow.add(menuWindowInstances);

    final JMenu menuWindowDiagram = super.createWindowMenu();
    menuWindowDiagram.setText(Messages.getString("Menu.diagrams"));
    menuWindow.add(menuWindowDiagram);

    return menuWindow;
}

From source file:com.googlecode.bpmn_simulator.gui.BPMNSimulatorFrame.java

private JMenu createMenuFile() {
    final JMenu menuFile = new JMenu(Messages.getString("Menu.file")); //$NON-NLS-1$

    final JMenuItem menuFileOpen = new JMenuItem(Messages.getString("Menu.fileOpen")); //$NON-NLS-1$
    menuFileOpen.setMnemonic(KeyEvent.VK_O);
    menuFileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.ALT_MASK));
    menuFileOpen.addActionListener(new ActionListener() {
        @Override//from w w  w.  j ava  2  s .  co  m
        public void actionPerformed(final ActionEvent e) {
            openFile();
        }
    });
    menuFile.add(menuFileOpen);
    menuFile.add(menuFileRecent);

    final JMenuItem menuFileReload = new JMenuItem(Messages.getString("Menu.fileReload")); //$NON-NLS-1$
    menuFileReload.setMnemonic(KeyEvent.VK_R);
    menuFileReload.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.ALT_MASK));
    menuFileReload.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            reloadDefinition();
        }
    });
    menuFile.add(menuFileReload);

    final JMenuItem menuFileClose = new JMenuItem(Messages.getString("Menu.fileClose")); //$NON-NLS-1$
    menuFileClose.setMnemonic(KeyEvent.VK_C);
    menuFileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.ALT_MASK));
    menuFileClose.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            closeSource();
        }
    });
    menuFile.add(menuFileClose);

    menuFile.addSeparator();

    final JMenuItem menuFileProperties = new JMenuItem(Messages.getString("Menu.properties")); //$NON-NLS-1$
    menuFileProperties.setMnemonic(KeyEvent.VK_P);
    menuFileProperties.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.ALT_MASK));
    menuFileProperties.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            showPropertiesDialog();
        }
    });
    menuFile.add(menuFileProperties);

    menuFile.addSeparator();

    final JMenuItem menuFileExport = createMenuFileExport();
    menuFile.add(menuFileExport);

    menuFile.addSeparator();

    final JMenuItem menuFilePreferences = new JMenuItem(Messages.getString("Menu.preferences")); //$NON-NLS-1$
    menuFilePreferences.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            showPreferencesDialog();
        }
    });
    menuFile.add(menuFilePreferences);

    menuFile.addSeparator();

    final JMenuItem menuFileExit = new JMenuItem(Messages.getString("Menu.exit")); //$NON-NLS-1$
    menuFileExit.setMnemonic(KeyEvent.VK_E);
    menuFileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.ALT_MASK));
    menuFileExit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            for (Frame frame : getFrames()) {
                if (frame.isActive()) {
                    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
                }
            }
        }
    });
    menuFile.add(menuFileExit);

    menuFile.addMenuListener(new MenuListener() {
        @Override
        public void menuSelected(final MenuEvent e) {
            menuFileReload.setEnabled(isSourceOpen() && currentSource.canReopen());
            menuFileClose.setEnabled(isSourceOpen());
            menuFileProperties.setEnabled(isDefinitionOpen());
            menuFileExport.setEnabled(isDefinitionOpen());
        }

        @Override
        public void menuDeselected(final MenuEvent e) {
        }

        @Override
        public void menuCanceled(final MenuEvent e) {
        }
    });

    return menuFile;
}

From source file:com.evanbelcher.DrillBook.display.DBMenuBar.java

/**
 * Constructs DBMenuBar. Adds the menu and menuitems.
 *
 * @param graphicsRunner the JFrame that created this
 * @param desktop        the DBDesktopPane in the JFrame
 *///  w w w. ja  v a 2 s  .c  o m
public DBMenuBar(GraphicsRunner graphicsRunner, DBDesktopPane desktop) {
    super();

    gr = graphicsRunner;
    this.desktop = desktop;

    //Set up the menu
    JMenu menu = new JMenu("File");
    add(menu);

    //Set up the menu items.
    JMenuItem menuItem = new JMenuItem("New");
    menuItem.setMnemonic(KeyEvent.VK_N);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK));
    menuItem.setActionCommand("new");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Open");
    menuItem.setMnemonic(KeyEvent.VK_O);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
    menuItem.setActionCommand("open");
    menuItem.addActionListener(this);
    menuItem.setLayout(new MigLayout());
    menu.add(menuItem);

    menuItem = new JMenuItem("Save");
    menuItem.setMnemonic(KeyEvent.VK_S);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
    menuItem.setActionCommand("save");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Save As");
    menuItem.setActionCommand("saveas");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Print    Current Page");
    menuItem.setMnemonic(KeyEvent.VK_P);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_DOWN_MASK));
    menuItem.setActionCommand("printpage");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Print Show");
    menuItem.setActionCommand("printshow");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Print Dot Sheets");
    menuItem.setActionCommand("printdotsheets");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Quit");
    menuItem.setMnemonic(KeyEvent.VK_Q);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, InputEvent.CTRL_DOWN_MASK));
    menuItem.setActionCommand("quit");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menu = new JMenu("Edit");
    add(menu);

    menuItem = new JMenuItem("Undo");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_DOWN_MASK));
    menuItem.setActionCommand("undo");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Redo");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_DOWN_MASK));
    menuItem.setActionCommand("redo");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menu = new JMenu("Settings");
    add(menu);

    menuItem = new JMenuItem("Toggle Gridlines");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_DOWN_MASK));
    menuItem.setActionCommand("togglegrid");
    menuItem.addActionListener(this);
    menuItem.setForeground(Main.getState().getSettings().shouldShowGrid() ? Color.BLACK : Color.RED);
    menu.add(menuItem);

    menuItem = new JMenuItem("Toggle Dot Names");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_DOWN_MASK));
    menuItem.setActionCommand("togglenames");
    menuItem.addActionListener(this);
    menuItem.setForeground(Main.getState().getSettings().shouldShowNames() ? Color.BLACK : Color.RED);
    menu.add(menuItem);

    menuItem = new JMenuItem("Toggle Text Box");
    menuItem.setActionCommand("toggletext");
    menuItem.addActionListener(this);
    menuItem.setForeground(Main.getState().getSettings().shouldShowText() ? Color.BLACK : Color.RED);
    menu.add(menuItem);

    menuItem = new JMenuItem("Color Code Dots by Instrument");
    menuItem.setActionCommand("colordots");
    menuItem.addActionListener(this);
    menuItem.setForeground(Main.getState().getSettings().shouldColorDots() ? Color.BLACK : Color.RED);
    menu.add(menuItem);

    menuItem = new JMenuItem();
    menuItem.setText(Main.getState().getSettings().useCollegeHashes() ? "Change to High School Hashes"
            : "Change to College Hashes");
    menuItem.setActionCommand("changehash");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Change Font Size");
    menuItem.setActionCommand("fontsize");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //add(Box.createHorizontalStrut(menu.getPreferredSize().width));

    //add these to the menubar itself
    menuItem = new JMenuItem("Play");
    menuItem.setMaximumSize(new Dimension(menuItem.getPreferredSize().width, Integer.MAX_VALUE));
    menuItem.setActionCommand("play");
    menuItem.addActionListener(this);
    add(menuItem);

    add(Box.createHorizontalGlue());

    menu = new JMenu("Help");
    add(menu);

    menuItem = new JMenuItem("Help");
    menuItem.setActionCommand("help");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("About");
    menuItem.setActionCommand("about");
    menuItem.addActionListener(this);
    menu.add(menuItem);
}

From source file:ListCutPaste.java

/**
 * Create an Edit menu to support cut/copy/paste.
 *//*from   w w w. j  a v  a2 s .com*/
public JMenuBar createMenuBar() {
    JMenuItem menuItem = null;
    JMenuBar menuBar = new JMenuBar();
    JMenu mainMenu = new JMenu("Edit");
    mainMenu.setMnemonic(KeyEvent.VK_E);
    TransferActionListener actionListener = new TransferActionListener();

    menuItem = new JMenuItem("Cut");
    menuItem.setActionCommand((String) TransferHandler.getCutAction().getValue(Action.NAME));
    menuItem.addActionListener(actionListener);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
    menuItem.setMnemonic(KeyEvent.VK_T);
    mainMenu.add(menuItem);

    menuItem = new JMenuItem("Copy");
    menuItem.setActionCommand((String) TransferHandler.getCopyAction().getValue(Action.NAME));
    menuItem.addActionListener(actionListener);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
    menuItem.setMnemonic(KeyEvent.VK_C);
    mainMenu.add(menuItem);

    menuItem = new JMenuItem("Paste");
    menuItem.setActionCommand((String) TransferHandler.getPasteAction().getValue(Action.NAME));
    menuItem.addActionListener(actionListener);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
    menuItem.setMnemonic(KeyEvent.VK_P);
    mainMenu.add(menuItem);

    menuBar.add(mainMenu);
    return menuBar;
}

From source file:be.ac.ua.comp.scarletnebula.gui.windows.GUI.java

private JMenu getProviderMenu() {
    final JMenu providerMenu = new JMenu("Providers");
    providerMenu.setMnemonic(KeyEvent.VK_P);
    providerMenu.getAccessibleContext().setAccessibleDescription("Managing cloud providers.");

    final JMenuItem manageProvidersItem = new JMenuItem("Manage Providers");
    manageProvidersItem.setMnemonic(KeyEvent.VK_M);
    manageProvidersItem.addActionListener(new ActionListener() {
        @Override//w ww  .  java 2  s  .c  o m
        public void actionPerformed(final ActionEvent e) {
            new ManageProvidersWindow(GUI.this);
        }
    });

    providerMenu.add(manageProvidersItem);

    final JMenuItem detectAllUnlinkedInstances = new JMenuItem("Link/Unlink Instances");
    detectAllUnlinkedInstances.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            new LinkUnlinkWindow(GUI.this);
            // detectAllUnlinkedInstances();
        }
    });
    providerMenu.add(detectAllUnlinkedInstances);
    return providerMenu;
}