Example usage for javax.swing JMenu setMnemonic

List of usage examples for javax.swing JMenu setMnemonic

Introduction

In this page you can find the example usage for javax.swing JMenu 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:MenuDemo.java

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

    JMenu menuFile = new JMenu("File");
    menuFile.setMnemonic('f');

    JMenuItem menuItem = new JMenuItem("New");
    menuItem.setIcon(new ImageIcon("file_new.gif"));
    menuItem.setMnemonic('n');
    ActionListener lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("New");
        }/*from  ww w  .  j av  a  2s  .co  m*/
    };
    menuItem.addActionListener(lst);
    menuFile.add(menuItem);

    menuItem = new JMenuItem("Open...");
    menuItem.setIcon(new ImageIcon("file_open.gif"));
    menuItem.setMnemonic('o');
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            MenuDemo.this.repaint();
            if (fileChooser.showOpenDialog(MenuDemo.this) != JFileChooser.APPROVE_OPTION)
                return;
            System.out.println(fileChooser.getSelectedFile());
        }
    };
    menuItem.addActionListener(lst);
    menuFile.add(menuItem);

    menuItem = new JMenuItem("Save...");
    menuItem.setIcon(new ImageIcon("file_save.gif"));
    menuItem.setMnemonic('s');
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Save...");
        }
    };
    menuItem.addActionListener(lst);
    menuFile.add(menuItem);

    menuFile.addSeparator();

    menuItem = new JMenuItem("Exit");
    menuItem.setMnemonic('x');
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    };
    menuItem.addActionListener(lst);
    menuFile.add(menuItem);
    menuBar.add(menuFile);

    ActionListener fontListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            updateMonitor();
        }
    };

    JMenu mFont = new JMenu("Font");
    mFont.setMnemonic('o');

    ButtonGroup group = new ButtonGroup();
    menus = new JMenuItem[FontNames.length];
    for (int i = 0; i < FontNames.length; i++) {
        int m = i + 1;
        menus[i] = new JRadioButtonMenuItem(m + " " + FontNames[i]);
        boolean selected = (i == 0);
        menus[i].setSelected(selected);
        menus[i].setMnemonic('1' + i);
        menus[i].setFont(fontArray[i]);
        menus[i].addActionListener(fontListener);
        group.add(menus[i]);
        mFont.add(menus[i]);
    }

    mFont.addSeparator();

    boldMenuItem = new JCheckBoxMenuItem("Bold");
    boldMenuItem.setMnemonic('b');
    Font fn = fontArray[1].deriveFont(Font.BOLD);
    boldMenuItem.setFont(fn);
    boldMenuItem.setSelected(false);
    boldMenuItem.addActionListener(fontListener);
    mFont.add(boldMenuItem);

    italicMenuItem = new JCheckBoxMenuItem("Italic");
    italicMenuItem.setMnemonic('i');
    fn = fontArray[1].deriveFont(Font.ITALIC);
    italicMenuItem.setFont(fn);
    italicMenuItem.setSelected(false);
    italicMenuItem.addActionListener(fontListener);
    mFont.add(italicMenuItem);

    menuBar.add(mFont);

    return menuBar;
}

From source file:TextCutPaste.java

/**
 * Create an Edit menu to support cut/copy/paste.
 *//*from  w  ww  .java2  s  . co  m*/
public JMenuBar createMenuBar() {
    JMenuItem menuItem = null;
    JMenuBar menuBar = new JMenuBar();
    JMenu mainMenu = new JMenu("Edit");
    mainMenu.setMnemonic(KeyEvent.VK_E);

    menuItem = new JMenuItem(new DefaultEditorKit.CutAction());
    menuItem.setText("Cut");
    menuItem.setMnemonic(KeyEvent.VK_T);
    mainMenu.add(menuItem);

    menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
    menuItem.setText("Copy");
    menuItem.setMnemonic(KeyEvent.VK_C);
    mainMenu.add(menuItem);

    menuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
    menuItem.setText("Paste");
    menuItem.setMnemonic(KeyEvent.VK_P);
    mainMenu.add(menuItem);

    menuBar.add(mainMenu);
    return menuBar;
}

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();/*from w  ww .  java  2s  . 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.erdfelt.android.sdkfido.ui.SdkFidoFrame.java

private JMenu createFileMenu() {
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic('f');

    JMenuItem fileExit = new JMenuItem("Exit");
    fileExit.setMnemonic('x');
    fileExit.setActionCommand("exit");
    fileExit.addActionListener(actionMapper);
    fileMenu.add(fileExit);//from w w  w .j  ava 2  s.  c  om

    return fileMenu;
}

From source file:net.erdfelt.android.sdkfido.ui.SdkFidoFrame.java

private JMenu createViewMenu() {
    JMenu viewMenu = new JMenu("View");
    viewMenu.setMnemonic('v');

    JMenu lnfMenu = new JMenu("Look and Feel");
    lnfMenu.setMnemonic('f');

    ButtonGroup lnfGroup = new ButtonGroup();
    LookAndFeelInfo lnfs[] = UIManager.getInstalledLookAndFeels();
    String lnfCurrentName = null;
    LookAndFeel lnfCurrent = UIManager.getLookAndFeel();
    if (lnfCurrent != null) {
        lnfCurrentName = lnfCurrent.getClass().getName();
    }// w ww  .  j av  a 2  s  . c  o  m
    UISwitcher switcher = new UISwitcher();
    for (int i = 0; i < lnfs.length; i++) {
        JRadioButtonMenuItem lnfItem = new JRadioButtonMenuItem(lnfs[i].getName());
        lnfItem.addActionListener(switcher);
        lnfItem.setActionCommand(lnfs[i].getClassName());
        lnfGroup.add(lnfItem);
        lnfMenu.add(lnfItem);

        if (lnfs[i].getClassName().equals(lnfCurrentName)) {
            lnfGroup.setSelected(lnfItem.getModel(), true);
        }
    }
    viewMenu.add(lnfMenu);

    return viewMenu;
}

From source file:fi.smaa.jsmaa.gui.SMAATRIGUIFactory.java

@Override
protected JMenu buildResultsMenu() {
    JMenu resultsMenu = new JMenu("Results");
    resultsMenu.setMnemonic('r');
    JMenuItem racsItem = new JMenuItem("Category acceptability indices",
            ImageFactory.IMAGELOADER.getIcon(FileNames.ICON_RANKACCEPTABILITIES));
    racsItem.setMnemonic('r');

    racsItem.addActionListener(new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            Focuser.focus(tree, treeModel, treeModel.getCatAccNode());
        }//from   ww  w  .j ava 2s. c  o  m
    });

    resultsMenu.add(racsItem);
    return resultsMenu;
}

From source file:fi.smaa.jsmaa.gui.SMAATRIGUIFactory.java

private JMenu buildCategoryMenu() {
    JMenu categoryMenu = new JMenu("Categories");
    categoryMenu.setMnemonic('t');
    JMenuItem showItem = new JMenuItem("Show");
    showItem.setMnemonic('s');
    JMenuItem addCatButton = createAddCatMenuItem();

    showItem.addActionListener(new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            Focuser.focus(tree, treeModel, treeModel.getCategoriesNode());
        }/* ww  w.java 2s. c  o m*/
    });

    categoryMenu.add(showItem);
    categoryMenu.addSeparator();
    categoryMenu.add(addCatButton);
    return categoryMenu;
}

From source file:com.alvermont.javascript.tools.shell.ShellJSConsole.java

public ShellJSConsole(String[] args) {
    super("Rhino JavaScript Console");

    final JMenuBar menubar = new JMenuBar();
    createFileChooser();/*  w  ww  .j a va 2 s .co m*/

    final String[] fileItems = { "Load...", "Close" };
    final String[] fileCmds = { "Load", "Close" };
    final char[] fileShortCuts = { 'L', 'X' };
    final String[] editItems = { "Cut", "Copy", "Paste" };
    final char[] editShortCuts = { 'T', 'C', 'P' };
    final JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic('F');

    final JMenu editMenu = new JMenu("Edit");
    editMenu.setMnemonic('E');

    for (int i = 0; i < fileItems.length; ++i) {
        final JMenuItem item = new JMenuItem(fileItems[i], fileShortCuts[i]);
        item.setActionCommand(fileCmds[i]);
        item.addActionListener(this);
        fileMenu.add(item);
    }

    for (int i = 0; i < editItems.length; ++i) {
        final JMenuItem item = new JMenuItem(editItems[i], editShortCuts[i]);
        item.addActionListener(this);
        editMenu.add(item);
    }

    final ButtonGroup group = new ButtonGroup();
    menubar.add(fileMenu);
    menubar.add(editMenu);
    setJMenuBar(menubar);
    this.consoleTextArea = new ShellConsoleTextArea(args);

    final JScrollPane scroller = new JScrollPane(this.consoleTextArea);
    setContentPane(scroller);
    this.consoleTextArea.setRows(24);
    this.consoleTextArea.setColumns(80);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            setVisible(false);
        }
    });
    pack();
    setVisible(true);
    // System.setIn(consoleTextArea.getIn());
    // System.setOut(consoleTextArea.getOut());
    // System.setErr(consoleTextArea.getErr());
    ShellMain.setIn(this.consoleTextArea.getIn());
    ShellMain.setOut(this.consoleTextArea.getOut());
    ShellMain.setErr(this.consoleTextArea.getErr());

    // we don't do this here as we want to separate construction from
    // the run thread
    this.args = args;

    //ShellMain.exec(args);
}

From source file:com.net2plan.gui.GUINet2Plan.java

private static JMenuItem getCurrentMenu(JMenuBar menubar, JMenu parent, String itemName) {
    int pos = itemName.indexOf('|');
    if (pos == -1) {
        if (parent == null)
            throw new RuntimeException("Bad");

        JMenuItem menuItem = new JMenuItem(itemName);
        parent.add(menuItem);//  ww w .j a va2  s  . co  m

        return menuItem;
    } else {
        String parentName = itemName.substring(0, pos);
        JMenu new_parent = null;

        MenuElement[] children;
        if (menubar != null)
            children = menubar.getSubElements();
        else if (parent != null)
            children = parent.getSubElements();
        else
            throw new RuntimeException("Bad");

        for (MenuElement item : children) {
            if (!(item instanceof JMenu) || !((JMenu) item).getText().equalsIgnoreCase(parentName))
                continue;

            new_parent = (JMenu) item;
            break;
        }

        if (new_parent == null) {
            new_parent = new JMenu(parentName);

            if (menubar != null) {
                if (parentName.equals("Tools")) {
                    menubar.add(new_parent, 1);
                    new_parent.setMnemonic('T');
                } else {
                    menubar.add(new_parent, menubar.getComponentCount() - 1);
                }
            } else if (parent != null) {
                parent.add(new_parent);
            } else {
                throw new RuntimeException("Bad");
            }
        }

        String new_itemName = itemName.substring(pos + 1);

        return getCurrentMenu(null, new_parent, new_itemName);
    }
}

From source file:TopLevelTransferHandlerDemo.java

private JMenuBar createDummyMenuBar() {
    JMenuBar mb = new JMenuBar();
    mb.add(createDummyMenu("File"));
    mb.add(createDummyMenu("Edit"));
    mb.add(createDummyMenu("Search"));
    mb.add(createDummyMenu("View"));
    mb.add(createDummyMenu("Tools"));
    mb.add(createDummyMenu("Help"));

    JMenu demo = new JMenu("Demo");
    demo.setMnemonic(KeyEvent.VK_D);
    mb.add(demo);/*from  w w  w . j a va 2s . c o  m*/

    thItem = new JCheckBoxMenuItem("Use Top-Level TransferHandler");
    thItem.setMnemonic(KeyEvent.VK_T);
    demo.add(thItem);

    nullItem = new JCheckBoxMenuItem("Remove TransferHandler from List and Text");
    nullItem.setMnemonic(KeyEvent.VK_R);
    demo.add(nullItem);

    copyItem = new JCheckBoxMenuItem("Use COPY Action");
    copyItem.setMnemonic(KeyEvent.VK_C);
    demo.add(copyItem);

    return mb;
}