MenuShortcut class is a part of JavaAWT. MenuShortcut class implements menu shortcuts which are implemented using virtual keycodes. The MenuShortcut class represents a keyboard accelerator for a MenuItem.
Constructors of the class:
- MenuShortcut(int k): Creates a MenuShortcut object with specified key.
- MenuShortcut(int k, boolean b): Constructs a new MenuShortcut for the specified virtual keycode.
Commonly Used Methods:
| Methods | Explanation |
|---|---|
| equals(MenuShortcut s) | Returns whether this MenuShortcut is the same as another. |
| getKey() | Returns the raw keycode of this MenuShortcut. |
| hashCode() | Returns the hashcode of MenuShortcut. |
| usesShiftModifier() | Returns whether this MenuShortcut must be invoked using the SHIFT key. |
Below programs illustrate the MenuShortcut class:
- Java program to create a menubar and add MenuItems to it and also add MenuShortcut to MenuItems: In this program we will create a Frame named frame, MenuBar named menubar, a Menu named menu and three MenuItems named menuitem_1, menuitem_2, menuitem_3. We will also create three MenuShortcut named menushortcut_1, menushortcut_2, menushortcut_3 and specify their key to ‘A’, ‘B’, ‘C’. When ctrl+A will be pressed menuitem_1 will be invoked. Similarly, when ctrl+B, ctrl+C will be pressed then menuitem_2, menuitem_3 will be invoked respectively. ActionListener will be added to all the menuitems and a label will display which MenuItem is clicked. The MenuItems will be added to the menu the menu will be added to the menubar and the menubar and label will be added to the frame and the size of the frame will be set to the specified value and the show function will display the results.
Java
// Java program to create a menubar and add// menuitems to it and also add menushortcut// to MenuItemsimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;ÂÂpublicclassShortcut_1extendsÂFrameimplementsActionListener {   Â// menubar   ÂstaticMenuBar menubar;   Â// Menu   ÂstaticMenu menu;   Â// Menu items   ÂstaticMenuItem menuitem_1, menuitem_2, menuitem_3;   Â// create a frame   ÂstaticFrame frame;   Â// create label   ÂLabel label;   Â// default constructor   ÂShortcut_1()   Â{       Â// create a frame       Âframe =newFrame("MenuShortcut Demo");       Â// when exit button is pressed       Âframe.addWindowListener(newWindowAdapter() {           ÂpublicvoidwindowClosing(WindowEvent windowEvent)           Â{               ÂSystem.exit(0);           Â}       Â});       Â// create a menubar       Âmenubar =newMenuBar();       Â// create a menu       Âmenu =newMenu("Menu");       Â// create label       Âlabel =newLabel("Nothing Selected");       Â// create menu shortcuts       ÂMenuShortcut menushortcut_1 =       ÂnewMenuShortcut(KeyEvent.VK_A,false);       ÂMenuShortcut menushortcut_2 =       ÂnewMenuShortcut(KeyEvent.VK_B,false);       ÂMenuShortcut menushortcut_3 =       ÂnewMenuShortcut(KeyEvent.VK_C,false);       Â// create menuitems       Âmenuitem_1 =newMenuItem("MenuItem_1 ", menushortcut_1);       Âmenuitem_2 =newMenuItem("MenuItem_2 ", menushortcut_2);       Âmenuitem_3 =newMenuItem("MenuItem_3 ", menushortcut_3);       Â// add ActionListener to menuItems       Âmenuitem_1.addActionListener(this);       Âmenuitem_2.addActionListener(this);       Âmenuitem_3.addActionListener(this);       Â// add menu items to menu       Âmenu.add(menuitem_1);       Âmenu.add(menuitem_2);       Âmenu.add(menuitem_3);       Â// add menu to menu bar       Âmenubar.add(menu);       Â// add menubar to frame       Âframe.setMenuBar(menubar);       Â// add label       Âframe.add(label);       Â// set the size of the frame       Âframe.setSize(500,500);       Âframe.setVisible(true);   Â}   Â// when an action is performed   ÂpublicvoidactionPerformed(ActionEvent e)   Â{       ÂString s = e.getActionCommand();       Â// set the label to the menuItem       Â// that is selected       Âlabel.setText(s +" selected");   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// create an object       ÂShortcut_1 m =newShortcut_1();   Â}}Output:
- Java program to create a menubar and add MenuItems to it and also add MenuShortcut to MenuItems and also add shift modifier to MenuShortcut: In this program we will create a Frame named frame, MenuBar named menubar, a Menu named menu and three MenuItems named menuitem_1, menuitem_2, menuitem_3. We will also create three MenuShortcut named menushortcut_1, menushortcut_2, menushortcut_3 and specify their key to ‘A’, ‘B’, ‘C’ and the shift modifier will be set to true. When ctrl+shift+A will be pressed menuitem_1 will be invoked. Similarly, when ctrl+shift+B, ctrl+shift+C will be pressed menuitem_2, menuitem_3 will be invoked respectively. ActionListener will be added to all the Menu Items and a label will display which MenuItem is clicked. The MenuItems will be added to the menu and the menu will be added to the menubar and the menubar and label will be added to the frame and the size of the frame will be set to the specified value and the show function will display the results.
Java
// Java program to create a menubar and addÂ// menuitems to it and also add menushortcutÂ// to MenuItems and also add shift modifier// to MenuShortcutimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;ÂÂpublicclassShortcut_2extendsFrame         ÂimplementsActionListener {   Â// menubar   ÂstaticMenuBar menubar;   Â// Menu   ÂstaticMenu menu;   Â// Menu items   ÂstaticMenuItem menuitem_1, menuitem_2, menuitem_3;   Â// create a frame   ÂstaticFrame frame;   Â// create label   ÂLabel label;   Â// default constructor   ÂShortcut_2()   Â{       Â// create a frame       Âframe =newFrame("MenuShortcut Demo");       Â// when exit button is pressed       Âframe.addWindowListener(newWindowAdapter() {           ÂpublicvoidwindowClosing(WindowEvent windowEvent)           Â{               ÂSystem.exit(0);           Â}       Â});       Â// create a menubar       Âmenubar =newMenuBar();       Â// create a menu       Âmenu =newMenu("Menu");       Â// create label       Âlabel =newLabel("Nothing Selected");       Â// create menu shortcuts       ÂMenuShortcut menushortcut_1 =       ÂnewMenuShortcut(KeyEvent.VK_A,true);       ÂMenuShortcut menushortcut_2 =       ÂnewMenuShortcut(KeyEvent.VK_B,true);       ÂMenuShortcut menushortcut_3 =       ÂnewMenuShortcut(KeyEvent.VK_C,true);       Â// create menuitems       Âmenuitem_1 =newMenuItem("MenuItem_1 ",                               Âmenushortcut_1);       Âmenuitem_2 =newMenuItem("MenuItem_2 ",                               Âmenushortcut_2);       Âmenuitem_3 =newMenuItem("MenuItem_3 ",                               Âmenushortcut_3);       Â// add ActionListener to menuItems       Âmenuitem_1.addActionListener(this);       Âmenuitem_2.addActionListener(this);       Âmenuitem_3.addActionListener(this);       Â// add menu items to menu       Âmenu.add(menuitem_1);       Âmenu.add(menuitem_2);       Âmenu.add(menuitem_3);       Â// add menu to menu bar       Âmenubar.add(menu);       Â// add menubar to frame       Âframe.setMenuBar(menubar);       Â// add label       Âframe.add(label);       Â// set the size of the frame       Âframe.setSize(500,500);       Âframe.setVisible(true);   Â}   Â// when an action is performed   ÂpublicvoidactionPerformed(ActionEvent e)   Â{       ÂString s = e.getActionCommand();       Â// set the label to the MenuItem,       Â// that is selected       Âlabel.setText(s +" selected");   Â}   Â// Main Function   Âpublicstaticvoidmain(String args[])   Â{       Â// create an object       ÂShortcut_2 m =newShortcut_2();   Â}}Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/7/docs/api/java/awt/MenuShortcut.html
