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

public static void main(String[] args) {
    JFrame f = new JFrame();
    JMenuBar menubar = new JMenuBar();
    ImageIcon iconNew = new ImageIcon("new.png");
    ImageIcon iconOpen = new ImageIcon("open.png");
    ImageIcon iconSave = new ImageIcon("save.png");
    ImageIcon iconClose = new ImageIcon("exit.png");

    JMenu file = new JMenu("File");
    JMenu imp = new JMenu("Import");

    JMenuItem fileNew = new JMenuItem("New", iconNew);
    JMenuItem fileOpen = new JMenuItem("Open", iconOpen);
    JMenuItem fileSave = new JMenuItem("Save", iconSave);
    JMenuItem fileClose = new JMenuItem("Close", iconClose);

    file.setMnemonic(KeyEvent.VK_F);
    imp.setMnemonic(KeyEvent.VK_M);

    JMenuItem newsf = new JMenuItem("Import newsfeed list...");
    JMenuItem bookm = new JMenuItem("Import bookmarks...");
    JMenuItem mail = new JMenuItem("Import mail...");

    imp.add(newsf);/*  w  w w.  j a  v  a2 s .  c  o m*/
    imp.add(bookm);
    imp.add(mail);

    fileNew.setMnemonic(KeyEvent.VK_N);
    fileNew.setMnemonic(KeyEvent.VK_O);
    fileSave.setMnemonic(KeyEvent.VK_S);

    fileClose.setMnemonic(KeyEvent.VK_C);
    fileClose.setToolTipText("Exit application");
    fileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));

    fileClose.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    file.add(fileNew);
    file.add(fileOpen);
    file.add(fileSave);
    file.addSeparator();
    file.add(imp);
    file.addSeparator();
    file.add(fileClose);

    menubar.add(file);

    f.setJMenuBar(menubar);

    f.setSize(360, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:MenuDemo1.java

public static JMenuItem menuItem(String label, ActionListener listener, String command, int mnemonic,
        int acceleratorKey) {
    JMenuItem item = new JMenuItem(label);
    item.addActionListener(listener);/*from ww w  .j  a  va 2s . co  m*/
    item.setActionCommand(command);
    if (mnemonic != 0)
        item.setMnemonic((char) mnemonic);
    if (acceleratorKey != 0)
        item.setAccelerator(KeyStroke.getKeyStroke(acceleratorKey, java.awt.Event.CTRL_MASK));
    return item;
}

From source file:Main.java

/** Brings the destination menu item into sync with the source item. */
protected static void syncMenuItem(final JMenuItem source, final JMenuItem dest) {
    final boolean enabled = source.isEnabled();
    if (dest.isEnabled() != enabled)
        dest.setEnabled(enabled);/*from  w w  w  . j  a  v a  2s  .  c  o  m*/
    final int mnemonic = source.getMnemonic();
    if (dest.getMnemonic() != mnemonic)
        dest.setMnemonic(mnemonic);
    final String text = source.getText();
    if (dest.getText() != text)
        dest.setText(text);
    final KeyStroke accel = source.getAccelerator();
    if (dest.getAccelerator() != accel)
        dest.setAccelerator(accel);
}

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);/* www  .  j  a  v  a 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. java 2  s  . c om*/
    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:MainClass.java

public MainClass() {
    menuBar = new JMenuBar();
    JMenu formatMenu = new JMenu("Justify");
    formatMenu.setMnemonic('J');

    MenuAction leftJustifyAction = new MenuAction("Left", new ImageIcon("left.gif"));
    MenuAction rightJustifyAction = new MenuAction("Right", new ImageIcon("right.gif"));
    MenuAction centerJustifyAction = new MenuAction("Center", new ImageIcon("center.gif"));
    MenuAction fullJustifyAction = new MenuAction("Full", new ImageIcon("full.gif"));

    JMenuItem item;
    item = formatMenu.add(leftJustifyAction);
    item.setMnemonic('L');
    item = formatMenu.add(rightJustifyAction);
    item.setMnemonic('R');
    item = formatMenu.add(centerJustifyAction);
    item.setMnemonic('C');
    item = formatMenu.add(fullJustifyAction);
    item.setMnemonic('F');

    menuBar.add(formatMenu);/*  w ww .  j  av a 2 s.  co  m*/
    menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));

}

From source file:MenuExample.java

public MenuExample() {
    menuBar = new JMenuBar();
    JMenu formatMenu = new JMenu("Justify");
    formatMenu.setMnemonic('J');

    MenuAction leftJustifyAction = new MenuAction("Left", new ImageIcon("1.gif"));
    MenuAction rightJustifyAction = new MenuAction("Right", new ImageIcon("2.gif"));
    MenuAction centerJustifyAction = new MenuAction("Center", new ImageIcon("3.gif"));
    MenuAction fullJustifyAction = new MenuAction("Full", new ImageIcon("4.gif"));

    JMenuItem item;
    item = formatMenu.add(leftJustifyAction);
    item.setMnemonic('L');
    item = formatMenu.add(rightJustifyAction);
    item.setMnemonic('R');
    item = formatMenu.add(centerJustifyAction);
    item.setMnemonic('C');
    item = formatMenu.add(fullJustifyAction);
    item.setMnemonic('F');

    menuBar.add(formatMenu);//from   w w  w.  j  a v  a2s  .c  o  m
    menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));

}

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 w  ww  .jav a2  s.com*/

    //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:Java2DExample.java

public JMenuItem createMenuItem(String menuItemName, char mnemonic, final MyFilter filter) {
    JMenuItem menuItem = new JMenuItem(menuItemName);
    menuItem.setMnemonic(mnemonic);
    menuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent action) {
            imagePanel.applyFilter(filter);
        }//from  w  ww  .j a v  a2s  . c  o  m

    });
    return menuItem;
}

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);//  w ww. j  a  v  a2 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;
}