[ Back | Previous | Next ]

Using the MenuShortcut in menus.

Package:
java.awt.*
Product:
JDK
Release:
1.1.x
Related Links:
Color
Cursor
FileDialog
Font
FontMetrics
Frame
general
Image
LayoutManager
Menu
ScrollPane
Comment:
What it not is: it is not the underlined key that appears in the label of a menu item (f.e. File )
What it is: The MenuShortcut is the shortcut key in a menu that appears after the label of a menu item (f.e. File Ctrl+N )

How do we implement MenuShortcuts?
We can implement MenuShortcuts in the MenuItem which are part of the Menu. The following source creates a menubar

  1. with a "File" menu with the menuitem "Open" and has a shortcut to the control+n key combination.
  2. with a "File" menu with the menuitem "Save" and has a shortcut to the shift+control+s key combination.
  3. import java.awt.*;
    import java.awt.event.*;
    ..class ... implements ActionListener {
          Frame f = new Frame("test");
          
          Menu m = new Menu("File");
          MenuItem open = new MenuItem( "Open", new MenuShortcut( KeyEvent.VK_O );
          m.add( open );
          open.addActionListener( this );
          MenuItem open = new MenuItem( "Save", new MenuShortcut( KeyEvent.VK_S, true );
          m.add( save );
          save.addActionListener( this );
          
          f.setMenuBar( m );
    
1