Skip to content
Related Articles

Related Articles

Java AWT | MenuShortcut Class
  • Last Updated : 23 Apr, 2021

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: 
 

  1. MenuShortcut(int k): Creates a MenuShortcut object with specified key.
  2. MenuShortcut(int k, boolean b): Constructs a new MenuShortcut for the specified virtual keycode.

Commonly Used Methods: 

 

MethodsExplanation
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: 
 

  1. 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 MenuItems
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
 
public class Shortcut_1 extends
Frame implements ActionListener {
 
    // menubar
    static MenuBar menubar;
 
    // Menu
    static Menu menu;
 
    // Menu items
    static MenuItem menuitem_1, menuitem_2, menuitem_3;
 
    // create a frame
    static Frame frame;
 
    // create label
    Label label;
 
    // default constructor
    Shortcut_1()
    {
 
        // create a frame
        frame = new Frame("MenuShortcut Demo");
 
        // when exit button is pressed
        frame.addWindowListener(new WindowAdapter() {
 
            public void windowClosing(WindowEvent windowEvent)
            {
                System.exit(0);
            }
        });
 
        // create a menubar
        menubar = new MenuBar();
 
        // create a menu
        menu = new Menu("Menu");
 
        // create label
        label = new Label("Nothing Selected");
 
        // create menu shortcuts
        MenuShortcut menushortcut_1 =
        new MenuShortcut(KeyEvent.VK_A, false);
 
        MenuShortcut menushortcut_2 =
        new MenuShortcut(KeyEvent.VK_B, false);
 
        MenuShortcut menushortcut_3 =
        new MenuShortcut(KeyEvent.VK_C, false);
 
        // create menuitems
        menuitem_1 = new MenuItem("MenuItem_1 ", menushortcut_1);
        menuitem_2 = new MenuItem("MenuItem_2 ", menushortcut_2);
        menuitem_3 = new MenuItem("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
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
 
        // set the label to the menuItem
        // that is selected
        label.setText(s + " selected");
    }
 
    // Main Method
    public static void main(String args[])
    {
 
        // create an object
        Shortcut_1 m = new Shortcut_1();
    }
}
  1. Output:

     

  1. 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 MenuShortcut
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
 
public class Shortcut_2 extends Frame
          implements ActionListener {
 
    // menubar
    static MenuBar menubar;
 
    // Menu
    static Menu menu;
 
    // Menu items
    static MenuItem menuitem_1, menuitem_2, menuitem_3;
 
    // create a frame
    static Frame frame;
 
    // create label
    Label label;
 
    // default constructor
    Shortcut_2()
    {
        // create a frame
        frame = new Frame("MenuShortcut Demo");
 
        // when exit button is prssed
        frame.addWindowListener(new WindowAdapter() {
 
            public void windowClosing(WindowEvent windowEvent)
            {
                System.exit(0);
            }
        });
 
        // create a menubar
        menubar = new MenuBar();
 
        // create a menu
        menu = new Menu("Menu");
 
        // create label
        label = new Label("Nothing Selected");
 
        // create menu shortcuts
        MenuShortcut menushortcut_1 =
        new MenuShortcut(KeyEvent.VK_A, true);
 
        MenuShortcut menushortcut_2 =
        new MenuShortcut(KeyEvent.VK_B, true);
 
        MenuShortcut menushortcut_3 =
        new MenuShortcut(KeyEvent.VK_C, true);
 
        // create menuitems
        menuitem_1 = new MenuItem("MenuItem_1 ",
                                menushortcut_1);
 
        menuitem_2 = new MenuItem("MenuItem_2 ",
                                menushortcut_2);
 
        menuitem_3 = new MenuItem("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
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
 
        // set the label to the MenuItem,
        // that is selected
        label.setText(s + " selected");
    }
 
    // Main Function
    public static void main(String args[])
    {
 
        // create an object
        Shortcut_2 m = new Shortcut_2();
    }
}
  1. 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
 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :