Using Shortcut for AWT MenuItem

The following illustrates using shortcut for AWT MenuItem with which you can fire an ActionEvent on the MenuItem.

import java.awt.*;
import java.awt.event.*;
class MenuItemShortcut extends Frame implements ActionListener
MenuBar mb;
Menu m;
MenuItem exit;
MenuShortcut s;

    public MenuItemShortcut()
    private void createAndShowGUI()
        setTitle("Shortcut for MenuItem");
        // Create MenuBar, Menu, MenuItem
        mb=new MenuBar();
        m=new Menu("Menu");
        exit=new MenuItem("Exit");
        // The shortcut is Ctrl+x
        // Ctrl is automatically added for MenuItem
        s=new MenuShortcut(KeyEvent.VK_X);
        // Set the shortuct to exit
        // Add ActionListener
        // Add exit to menu and menu to menubar
        // Set menu bar to frame
    public void actionPerformed(ActionEvent ae)
        // Terminate the program
    public static void main(String args[])
        new MenuItemShortcut();

Whenever user presses Ctrl+x then ActionEvent is fired on exit MenuItem and program is terminated.

Using Shortcut for AWT MenuItem

Next: Using KeyListener for AWT TextField
Previous: Using ActionListener for AWT List

Posted by at on

Tags: AWT Event Handling,