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:MenuAcceleratorKeyStroke.java

public MenuAcceleratorKeyStroke() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menu.setMnemonic('f');
    bar.add(menu);// w w w. j a  va  2s.c o  m

    JMenuItem exit = new JMenuItem("Exit");
    exit.setMnemonic('x');
    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Exit performed");
            MenuAcceleratorKeyStroke.this.dispose();
            System.exit(0);
        }
    });
    menu.add(exit);

    menu = new JMenu("Edit");
    menu.setMnemonic('e');
    bar.add(menu);

    EditListener l = new EditListener();
    JMenuItem mi;
    mi = menu.add(new JMenuItem("Cut", 't'));
    mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
    mi.addActionListener(l);
    mi = menu.add(new JMenuItem("Copy", 'c'));
    mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
    mi.addActionListener(l);
    mi = menu.add(new JMenuItem("Paste", 'p'));
    mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK));
    mi.addActionListener(l);

    setJMenuBar(bar);
    getContentPane().add(new JLabel("A placeholder"));

    pack();
    setSize(300, 300);
    setVisible(true);
}

From source file:Main.java

public JMenuBar createMenuBar() {
    JMenuBar top_menu_bar = new JMenuBar();
    JMenu main_menu = new JMenu("Menu");
    main_menu.setMnemonic(KeyEvent.VK_M);
    top_menu_bar.add(main_menu);//from ww w  .  j av  a2s  . co m
    JMenuItem menu_item;

    menu_item = new JMenuItem("Add New");
    menu_item.setMnemonic(KeyEvent.VK_N);
    menu_item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
    menu_item.setActionCommand("new");
    menu_item.addActionListener(e -> createThumb());
    main_menu.add(menu_item);
    return top_menu_bar;
}

From source file:components.TabComponentsDemo.java

private void initMenu() {
    JMenuBar menuBar = new JMenuBar();
    //create Options menu
    tabComponentsItem = new JCheckBoxMenuItem("Use TabComponents", true);
    tabComponentsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.ALT_MASK));
    tabComponentsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < pane.getTabCount(); i++) {
                if (tabComponentsItem.isSelected()) {
                    initTabComponent(i);
                } else {
                    pane.setTabComponentAt(i, null);
                }//from w  w w  . j ava2 s .c o m
            }
        }
    });
    scrollLayoutItem = new JCheckBoxMenuItem("Set ScrollLayout");
    scrollLayoutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.ALT_MASK));
    scrollLayoutItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (pane.getTabLayoutPolicy() == JTabbedPane.WRAP_TAB_LAYOUT) {
                pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
            } else {
                pane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
            }
        }
    });
    JMenuItem resetItem = new JMenuItem("Reset JTabbedPane");
    resetItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.ALT_MASK));
    resetItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            runTest();
        }
    });

    JMenu optionsMenu = new JMenu("Options");
    optionsMenu.add(tabComponentsItem);
    optionsMenu.add(scrollLayoutItem);
    optionsMenu.add(resetItem);
    menuBar.add(optionsMenu);
    setJMenuBar(menuBar);
}

From source file:components.InternalFrameDemo.java

protected JMenuBar createMenuBar() {
    JMenuBar menuBar = new JMenuBar();

    //Set up the lone menu.
    JMenu menu = new JMenu("Document");
    menu.setMnemonic(KeyEvent.VK_D);
    menuBar.add(menu);//from   ww w  . ja  v  a 2  s  .c om

    //Set up the first menu item.
    JMenuItem menuItem = new JMenuItem("New");
    menuItem.setMnemonic(KeyEvent.VK_N);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("new");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //Set up the second menu item.
    menuItem = new JMenuItem("Quit");
    menuItem.setMnemonic(KeyEvent.VK_Q);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("quit");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    return menuBar;
}

From source file:InternalFrameDemo.java

protected JMenuBar createMenuBar() {
    JMenuBar menuBar = new JMenuBar();

    // Set up the lone menu.
    JMenu menu = new JMenu("Document");
    menu.setMnemonic(KeyEvent.VK_D);
    menuBar.add(menu);//www  .ja v a  2s.c om

    // Set up the first menu item.
    JMenuItem menuItem = new JMenuItem("New");
    menuItem.setMnemonic(KeyEvent.VK_N);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("new");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    // Set up the second menu item.
    menuItem = new JMenuItem("Quit");
    menuItem.setMnemonic(KeyEvent.VK_Q);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("quit");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    return menuBar;
}

From source file:net.sf.clichart.main.ChartFrame.java

private void createMenu() {
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);//ww w  . j a  v  a 2  s  .c  om

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

    JMenuItem saveItem = new JMenuItem("Save as...", KeyEvent.VK_S);
    saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
    saveItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            saveChart();
        }
    });
    fileMenu.add(saveItem);

    JMenuItem exitItem = new JMenuItem("Exit", KeyEvent.VK_Q);
    exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    fileMenu.addSeparator();
    fileMenu.add(exitItem);

}

From source file:net.pandoragames.far.ui.swing.component.UndoHistoryPopupMenu.java

private void init(SwingConfig config, ComponentRepository componentRepository) {
    // COPY//ww  w .  java  2  s.  c  o  m
    JMenuItem copy = new JMenuItem(config.getLocalizer().localize("label.copy"));
    copy.setAccelerator(KeyStroke.getKeyStroke("control C"));
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String selection = textComponent.getSelectedText();
            if (selection != null) {
                StringSelection textTransfer = new StringSelection(selection);
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(textTransfer, null);
            }
        }
    });
    this.add(copy);
    // PASTE
    JMenuItem paste = new JMenuItem(config.getLocalizer().localize("label.paste"));
    paste.setAccelerator(KeyStroke.getKeyStroke("control V"));
    paste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Transferable transfer = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

            try {
                if (transfer != null && transfer.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    String text = (String) transfer.getTransferData(DataFlavor.stringFlavor);
                    String selected = textComponent.getSelectedText();
                    if (selected == null) {
                        int insertAt = textComponent.getCaretPosition();
                        textComponent.getDocument().insertString(insertAt, text, null);
                    } else {
                        int start = textComponent.getSelectionStart();
                        int end = textComponent.getSelectionEnd();
                        textComponent.getDocument().remove(start, end - start);
                        textComponent.getDocument().insertString(start, text, null);
                    }
                }
            } catch (UnsupportedFlavorException e) {
                LogFactory.getLog(this.getClass()).error("UnsupportedFlavorException reading from clipboard",
                        e);
            } catch (IOException iox) {
                LogFactory.getLog(this.getClass()).error("IOException reading from clipboard", iox);
            } catch (BadLocationException blx) {
                LogFactory.getLog(this.getClass()).error("BadLocationException reading from clipboard", blx);
            }
        }
    });
    this.add(paste);
    // UNDO
    Action undoAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_UNDO);
    if (undoAction != null) {
        undoAction.putValue(Action.NAME, config.getLocalizer().localize("label.undo"));
        this.add(undoAction);
    }
    // REDO
    Action redoAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_REDO);
    if (redoAction != null) {
        redoAction.putValue(Action.NAME, config.getLocalizer().localize("label.redo"));
        this.add(redoAction);
    }
    // PREVIOUS
    Action prevAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_PREVIOUS);
    if (prevAction != null) {
        prevAction.putValue(Action.NAME, config.getLocalizer().localize("label.previous"));
        this.add(prevAction);
    }
    // NEXT
    Action nextAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_NEXT);
    if (nextAction != null) {
        nextAction.putValue(Action.NAME, config.getLocalizer().localize("label.next"));
        this.add(nextAction);
    }
}

From source file:MenuTest.java

public MenuFrame() {
    setTitle("MenuTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    JMenu fileMenu = new JMenu("File");
    fileMenu.add(new TestAction("New"));

    // demonstrate accelerators

    JMenuItem openItem = fileMenu.add(new TestAction("Open"));
    openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));

    fileMenu.addSeparator();// w w w.  ja v a 2 s  . c o m

    saveAction = new TestAction("Save");
    JMenuItem saveItem = fileMenu.add(saveAction);
    saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));

    saveAsAction = new TestAction("Save As");
    fileMenu.add(saveAsAction);
    fileMenu.addSeparator();

    fileMenu.add(new AbstractAction("Exit") {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    // demonstrate check box and radio button menus

    readonlyItem = new JCheckBoxMenuItem("Read-only");
    readonlyItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            boolean saveOk = !readonlyItem.isSelected();
            saveAction.setEnabled(saveOk);
            saveAsAction.setEnabled(saveOk);
        }
    });

    ButtonGroup group = new ButtonGroup();

    JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
    insertItem.setSelected(true);
    JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype");

    group.add(insertItem);
    group.add(overtypeItem);

    // demonstrate icons

    Action cutAction = new TestAction("Cut");
    cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif"));
    Action copyAction = new TestAction("Copy");
    copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));
    Action pasteAction = new TestAction("Paste");
    pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));

    JMenu editMenu = new JMenu("Edit");
    editMenu.add(cutAction);
    editMenu.add(copyAction);
    editMenu.add(pasteAction);

    // demonstrate nested menus

    JMenu optionMenu = new JMenu("Options");

    optionMenu.add(readonlyItem);
    optionMenu.addSeparator();
    optionMenu.add(insertItem);
    optionMenu.add(overtypeItem);

    editMenu.addSeparator();
    editMenu.add(optionMenu);

    // demonstrate mnemonics

    JMenu helpMenu = new JMenu("Help");
    helpMenu.setMnemonic('H');

    JMenuItem indexItem = new JMenuItem("Index");
    indexItem.setMnemonic('I');
    helpMenu.add(indexItem);

    // you can also add the mnemonic key to an action
    Action aboutAction = new TestAction("About");
    aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
    helpMenu.add(aboutAction);

    // add all top-level menus to menu bar

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    menuBar.add(helpMenu);

    // demonstrate pop-ups

    popup = new JPopupMenu();
    popup.add(cutAction);
    popup.add(copyAction);
    popup.add(pasteAction);

    JPanel panel = new JPanel();
    panel.setComponentPopupMenu(popup);
    add(panel);

    // the following line is a workaround for bug 4966109
    panel.addMouseListener(new MouseAdapter() {
    });
}

From source file:net.adamjak.thomas.graph.application.gui.ResultsWidnow.java

private JMenuBar createJMenuBar() {
    this.jMenuBar = new JMenuBar();

    // Menu File/*from   w w  w. j  a v a2  s  . c o m*/
    JMenu jmFile = new JMenu("File");

    JMenuItem jmiFileSaveResults = new JMenuItem("Save results");
    jmiFileSaveResults.setAccelerator(GuiAccelerators.CTRL_S);
    jmiFileSaveResults.addActionListener(new AlJmiFileSaveResults());

    jmFile.add(jmiFileSaveResults);

    //jmFile.addSeparator();

    this.jMenuBar.add(jmFile);

    return this.jMenuBar;
}

From source file:ec.ui.chart.RevisionChartPanel.java

private JMenu buildMenu() {
    am = new ActionMap();
    am.put(COPY_ACTION, new CopyAction());

    im = new InputMap();
    KeyStrokes.putAll(im, KeyStrokes.COPY, COPY_ACTION);

    JMenu result = new JMenu();

    JMenuItem item;

    item = new JMenuItem(am.get(COPY_ACTION));
    item.setText("Copy All");
    item.setAccelerator(KeyStrokes.COPY.get(0));
    ExtAction.hideWhenDisabled(item);//from  w w  w. ja  v a  2s  .  co m
    result.add(item);

    return result;
}