Example usage for javax.swing JMenuItem setAccelerator

List of usage examples for javax.swing JMenuItem setAccelerator

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "The keystroke combination which will invoke the JMenuItem's actionlisteners without navigating the menu hierarchy")
public void setAccelerator(KeyStroke keyStroke) 

Source Link

Document

Sets the key combination which invokes the menu item's action listeners without navigating the menu hierarchy.

Usage

From source file:misc.TextBatchPrintingDemo.java

/**
 * Create and return a menu item with the specified action, mnemonics and
 * keyboad accelerator./*from  ww  w .  j  a va2  s. c  o m*/
 */
JMenuItem createMenuItem(Action action, int mnemonics, KeyStroke accel) {
    JMenuItem item = new JMenuItem(action);
    item.setMnemonic(mnemonics);
    item.setAccelerator(accel);
    return item;
}

From source file:IntroExample.java

public IntroExample() {

    JMenu fileMenu = new JMenu("File");
    JMenu editMenu = new JMenu("Edit");
    JMenu otherMenu = new JMenu("Other");
    JMenu subMenu = new JMenu("SubMenu");
    JMenu subMenu2 = new JMenu("SubMenu2");

    //  Assemble the File menus with mnemonics
    ActionListener printListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.out.println("Menu item [" + event.getActionCommand() + "] was pressed.");
        }//from  w  ww  .  jav a  2s.com
    };
    for (int i = 0; i < fileItems.length; i++) {
        JMenuItem item = new JMenuItem(fileItems[i], fileShortcuts[i]);
        item.addActionListener(printListener);
        fileMenu.add(item);
    }

    //  Assemble the File menus with keyboard accelerators
    for (int i = 0; i < editItems.length; i++) {
        JMenuItem item = new JMenuItem(editItems[i]);
        item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i],
                Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
        item.addActionListener(printListener);
        editMenu.add(item);
    }

    //  Insert a separator in the Edit Menu in Position 1 after "Undo"
    editMenu.insertSeparator(1);

    //  Assemble the submenus of the Other Menu
    JMenuItem item;
    subMenu2.add(item = new JMenuItem("Extra 2"));
    item.addActionListener(printListener);
    subMenu.add(item = new JMenuItem("Extra 1"));
    item.addActionListener(printListener);
    subMenu.add(subMenu2);

    //  Assemble the Other Menu itself
    otherMenu.add(subMenu);
    otherMenu.add(item = new JCheckBoxMenuItem("Check Me"));
    item.addActionListener(printListener);
    otherMenu.addSeparator();
    ButtonGroup buttonGroup = new ButtonGroup();
    otherMenu.add(item = new JRadioButtonMenuItem("Radio 1"));
    item.addActionListener(printListener);
    buttonGroup.add(item);
    otherMenu.add(item = new JRadioButtonMenuItem("Radio 2"));
    item.addActionListener(printListener);
    buttonGroup.add(item);
    otherMenu.addSeparator();
    otherMenu.add(item = new JMenuItem("Potted Plant", new ImageIcon("image.gif")));
    item.addActionListener(printListener);

    //  Finally, add all the menus to the menu bar
    add(fileMenu);
    add(editMenu);
    add(otherMenu);
}

From source file:JXButtonPanel.java

public JXButtonPanelDemo() {
    super("JXButtonPanel demo");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);/*from  w w w.ja v a2 s. c  o  m*/
    JPanel topPanel = new JPanel(new GridLayout(1, 0));

    final JXButtonPanel radioGroupPanel = createRadioJXButtonPanel();
    topPanel.add(radioGroupPanel);

    final JXButtonPanel checkBoxPanel = createCheckBoxJXButtonPanel();
    topPanel.add(checkBoxPanel);

    add(topPanel);
    add(createButtonJXButtonPanel(), BorderLayout.SOUTH);
    pack();

    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("Options");
    JMenuItem item = new JMenuItem("Unselect radioButtons");
    item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.ALT_MASK));
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // hack for 1.5 
            // in 1.6 ButtonGroup.clearSelection added
            JRadioButton b = new JRadioButton();
            radioGroup.add(b);
            b.setSelected(true);
            radioGroup.remove(b);
        }
    });
    menu.add(item);
    bar.add(menu);
    setJMenuBar(bar);

    setSize(300, 300);
    setLocationRelativeTo(null);
}

From source file:MenuShortcuts.java

MenuShortcuts(String s) {
    super("JMenuShortcuts: " + s);
    mb = new JMenuBar();
    setJMenuBar(mb); // Frame implements JMenuContainer

    Container cp = getContentPane();
    JMenuItem mi;
    // The File JMenu...
    fm = new JMenu("File");
    fm.setMnemonic('F');
    fm.add(mi = new JMenuItem("Open", 'O'));
    mi.addActionListener(this);
    fm.add(mi = new JMenuItem("Close", 'W'));
    mi.addActionListener(this);
    fm.addSeparator();//from w  w w  . j  a  v  a  2  s .co m

    fm.add(mi = new JMenuItem("Print", 'P'));
    mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, Event.ALT_MASK));

    mi.addActionListener(this);
    fm.addSeparator();
    fm.add(mi = new JMenuItem("Exit", 'Q'));
    exitItem = mi; // save for action handler
    mi.addActionListener(this);
    mb.add(fm);

    // The Options JMenu...
    om = new JMenu("Options");
    om.add(new JMenuItem("Alpha"));
    om.add(new JMenuItem("Gamma"));
    om.add(new JMenuItem("Delta"));
    mb.add(om);

    // The Help JMenu...
    hm = new JMenu("Help");
    hm.add(mi = new JMenuItem("About"));
    mi.addActionListener(this);
    hm.add(mi = new JMenuItem("Topics"));
    mi.addActionListener(this);
    mb.add(hm);
    // mb.setHelpMenu(hm); // needed for portability (Motif, etc.).

    // the main window
    cp.add(new Label("Menu Demo Window", 200, 150));
    pack();
}

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

/**
 * Set up menus when running under Windows.
 *//*from w  w  w.  jav  a2s . co  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:com.haulmont.cuba.desktop.sys.MenuBuilder.java

private void assignShortcut(JMenuItem jMenuItem, MenuItem item) {
    if (item.getShortcut() != null) {
        KeyCombination.Key key = item.getShortcut().getKey();
        KeyCombination.Modifier[] modifiers = item.getShortcut().getModifiers();
        KeyCombination combo = new KeyCombination(key, modifiers);
        jMenuItem.setAccelerator(DesktopComponentsHelper.convertKeyCombination(combo));
    }//from  w  w w .j  a va 2 s .  co m
}

From source file:org.angnysa.yaba.swing.BudgetFrame.java

private void buildMenubar() {
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu(Messages.getString("menu.file.label")); //$NON-NLS-1$
    JMenuItem item;

    // new/*w  w w.  jav  a  2 s.  c  o  m*/
    item = new JMenuItem(Messages.getString("menu.file.new")); //$NON-NLS-1$
    item.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_N, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            SwingMain.newEditor(null);
        }
    });
    fileMenu.add(item);

    // open
    item = new JMenuItem(Messages.getString("menu.file.open")); //$NON-NLS-1$
    item.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            SwingMain.open(id);
        }
    });
    fileMenu.add(item);

    // save
    item = new JMenuItem(Messages.getString("menu.file.save")); //$NON-NLS-1$
    item.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            SwingMain.save(id);
        }
    });
    fileMenu.add(item);

    // save as
    item = new JMenuItem(Messages.getString("menu.file.save-as")); //$NON-NLS-1$
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            SwingMain.saveas(id);
        }
    });
    fileMenu.add(item);

    // close
    item = new JMenuItem(Messages.getString("menu.file.close")); //$NON-NLS-1$
    item.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    item.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            SwingMain.close(id);
        }
    });
    fileMenu.add(item);

    menuBar.add(fileMenu);
    setJMenuBar(menuBar);
}

From source file:io.github.dsheirer.gui.SDRTrunk.java

/**
 * Initialize the contents of the frame.
 *//*from w  ww.  j  a v  a 2  s . c om*/
private void initGUI() {
    mMainGui.setLayout(new MigLayout("insets 0 0 0 0 ", "[grow,fill]", "[grow,fill]"));

    /**
     * Setup main JFrame window
     */
    mTitle = SystemProperties.getInstance().getApplicationName();
    mMainGui.setTitle(mTitle);
    mMainGui.setBounds(100, 100, 1280, 800);
    mMainGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set preferred sizes to influence the split
    mSpectralPanel.setPreferredSize(new Dimension(1280, 300));
    mControllerPanel.setPreferredSize(new Dimension(1280, 500));

    mSplitPane = new JideSplitPane(JideSplitPane.VERTICAL_SPLIT);
    mSplitPane.setDividerSize(5);
    mSplitPane.add(mSpectralPanel);
    mSplitPane.add(mControllerPanel);

    mBroadcastStatusVisible = SystemProperties.getInstance().get(PROPERTY_BROADCAST_STATUS_VISIBLE, false);

    //Show broadcast status panel when user requests - disabled by default
    if (mBroadcastStatusVisible) {
        mSplitPane.add(getBroadcastStatusPanel());
    }

    mMainGui.add(mSplitPane, "cell 0 0,span,grow");

    /**
     * Menu items
     */
    JMenuBar menuBar = new JMenuBar();
    mMainGui.setJMenuBar(menuBar);

    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);

    JMenuItem logFilesMenu = new JMenuItem("Logs & Recordings");
    logFilesMenu.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            try {
                Desktop.getDesktop().open(getHomePath().toFile());
            } catch (Exception e) {
                mLog.error("Couldn't open file explorer");

                JOptionPane.showMessageDialog(mMainGui,
                        "Can't launch file explorer - files are located at: " + getHomePath().toString(),
                        "Can't launch file explorer", JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    fileMenu.add(logFilesMenu);

    JMenuItem settingsMenu = new JMenuItem("Icon Manager");
    settingsMenu.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            mIconManager.showEditor(mMainGui);
        }
    });
    fileMenu.add(settingsMenu);

    fileMenu.add(new JSeparator());

    JMenuItem exitMenu = new JMenuItem("Exit");
    exitMenu.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    fileMenu.add(exitMenu);

    JMenu viewMenu = new JMenu("View");

    viewMenu.add(new BroadcastStatusVisibleMenuItem(mControllerPanel));

    menuBar.add(viewMenu);

    JMenuItem screenCaptureItem = new JMenuItem("Screen Capture");

    screenCaptureItem.setMnemonic(KeyEvent.VK_C);
    screenCaptureItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.ALT_MASK));

    screenCaptureItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            try {
                Robot robot = new Robot();

                final BufferedImage image = robot.createScreenCapture(mMainGui.getBounds());

                SystemProperties props = SystemProperties.getInstance();

                Path capturePath = props.getApplicationFolder("screen_captures");

                if (!Files.exists(capturePath)) {
                    try {
                        Files.createDirectory(capturePath);
                    } catch (IOException e) {
                        mLog.error("Couldn't create 'screen_captures' " + "subdirectory in the "
                                + "SDRTrunk application directory", e);
                    }
                }

                String filename = TimeStamp.getTimeStamp("_") + "_screen_capture.png";

                final Path captureFile = capturePath.resolve(filename);

                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            ImageIO.write(image, "png", captureFile.toFile());
                        } catch (IOException e) {
                            mLog.error("Couldn't write screen capture to " + "file [" + captureFile.toString()
                                    + "]", e);
                        }
                    }
                });
            } catch (AWTException e) {
                mLog.error("Exception while taking screen capture", e);
            }
        }
    });

    menuBar.add(screenCaptureItem);
}

From source file:net.chaosserver.timelord.swingui.TimelordMenu.java

/**
 * Creates the task menu.//  ww  w .  j  a v  a 2s .c  om
 *
 * @return the new task menu
 */
protected JMenu createTaskMenu() {
    JMenuItem menuItem;
    JMenu taskMenu = new JMenu("Task");
    taskMenu.setMnemonic(KeyEvent.VK_T);
    this.add(taskMenu);

    menuItem = new JMenuItem("New Task", KeyEvent.VK_N);
    menuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_N, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    menuItem.setActionCommand(ACTION_ADDTASK);
    menuItem.addActionListener(this);
    taskMenu.add(menuItem);

    menuItem = new JMenuItem("Hide Old Tasks", KeyEvent.VK_H);
    menuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_H, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    menuItem.setActionCommand(ACTION_HIDETASK);
    menuItem.addActionListener(this);
    taskMenu.add(menuItem);

    menuItem = new JMenuItem("Unhide Task", KeyEvent.VK_U);
    menuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_U, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    menuItem.setActionCommand(ACTION_UNHIDETASK);
    menuItem.addActionListener(this);
    taskMenu.add(menuItem);

    taskMenu.addSeparator();

    menuItem = new JMenuItem("Add Time", KeyEvent.VK_A);
    menuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_A, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    menuItem.setActionCommand(ACTION_ADDTIME);
    menuItem.addActionListener(this);
    taskMenu.add(menuItem);

    menuItem = new JMenuItem("Find Task", KeyEvent.VK_F);
    menuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_F, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    menuItem.setActionCommand(ACTION_FINDTASK);
    menuItem.addActionListener(this);
    taskMenu.add(menuItem);

    return taskMenu;
}

From source file:net.chaosserver.timelord.swingui.TimelordMenu.java

/**
 * Creates a new instance of the help menu.
 *
 * @return the new help menu/*from   w w  w  .  j a  v a 2 s .c  o  m*/
 */
protected JMenu createHelpMenu() {
    JMenuItem menuItem;
    JMenu helpMenu = new JMenu("Help");
    helpMenu.setMnemonic(KeyEvent.VK_H);
    this.add(helpMenu);

    menuItem = new JMenuItem("Help Topics", KeyEvent.VK_H);
    menuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_H, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));

    try {
        URL hsURL = HelpSet.findHelpSet(this.getClass().getClassLoader(),
                "net/chaosserver/timelord/help/TimelordHelp.hs");
        HelpSet hs = new HelpSet(null, hsURL);
        HelpBroker hb = hs.createHelpBroker();
        menuItem.addActionListener(new CSH.DisplayHelpFromSource(hb));
    } catch (HelpSetException e) {
        menuItem.setEnabled(false);
    }

    // menuItem.setActionCommand(ACTION_ABOUT);
    helpMenu.add(menuItem);

    menuItem = new JMenuItem("Log Window");
    menuItem.setActionCommand(ACTION_LOG);
    menuItem.addActionListener(this);
    menuItem.setEnabled(false);
    helpMenu.add(menuItem);

    menuItem = new JMenuItem("Cause Memory Leak");
    menuItem.setActionCommand(ACTION_LEAK);
    menuItem.addActionListener(this);
    menuItem.setEnabled(false);
    helpMenu.add(menuItem);

    if (!OsUtil.isMac()) {
        helpMenu.addSeparator();

        menuItem = new JMenuItem("About Timelord", KeyEvent.VK_A);
        menuItem.setActionCommand(ACTION_ABOUT);
        menuItem.addActionListener(this);
        helpMenu.add(menuItem);
    }

    return helpMenu;
}