Make your own free website on Tripod.com

Javanook Home

Menu Shortcut

Examples >> Core | Menu Shortcuts


Objectives

listing 1

// JMenuShortcut.java
/*
* The code shows how to set the menu bar for a JFrame.
* The menu bar is set using the setJMenuBar method.
* To add a JMenu to a JMenuBar, we use the add(JMenu) method.
* To add menu items to a JMenu, we use the add(JMenuItem) method.
* This example shows 3 different ways to add shortcuts to menu & menuitem
*/

// use swing components
import javax.swing.*;

// use awt layouts, if any
import java.awt.*;

// use awt event listeners
import java.awt.event.*;

public class JMenuShortcut extends JFrame {

JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;

public JMenuShortcut() {

// Create & set the menu bar.
menuBar = new JMenuBar();
setJMenuBar(menuBar);

// Build the first menu.
menu = new JMenu("File");
// set shortcut using setMnemonic method, uses the ALT mask
menu.setMnemonic(KeyEvent.VK_F);
menuBar.add(menu);

/* Create some JMenuItems */

// create a menu item
menuItem = new JMenuItem("New");
// set keyboard shortcut using setAccelerator method
// use with Control button
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N, ActionEvent.CTRL_MASK));
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("Menu item 'New' clicked.");
}
});
// add to menu
menu.add(menuItem);

// add a separator
menu.addSeparator();

// create another menu item
// combines creation and adding shortcut
/* Use Alt+F+X to exit - Windows style */
menuItem = new JMenuItem("Exit", KeyEvent.VK_X);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("Menu item 'Exit' clicked.");
System.exit(0);
}
});
// add to menu
menu.add(menuItem);

// add a window listener to the frame to enable closing
addWindowListener(new WindowAdapter() { // anonymous inner class
public void windowClosing(WindowEvent evt) {
// exit program
System.exit(0);
}
});

setTitle("JMenuShortcut Example");
setSize(300,200);
setVisible(true);
}

// test drive the program
public static void main(String[] args) {

new JMenuShortcut();
}
}

The JMenuShortcut class explains clearly how to create menus using Java Swing API. It shows how to create Menu items and their keyboard shortcuts. The example shows different ways to assign keyboard shortcuts to menu items.

The class has a main method to test the program. Just copy and paste the code in your favorite java code editor. Then, compile and run it from the command line as

java JMenuShortcut

You will see a small window pop up on the screen. Click on the menu and the menu items, either using the mouse or the keyboard shortcuts provided. Observe the results on the command console. Here is the screen shot of the output on my system.

A note on the JMenu class

The JMenu and the JMenuItem classes derive from the JAbstractButton class. JMenuBar is a subclass of JComponent. It hold the menus and their menu items. 

JPopupMenu is a subclass of JComponent. When a menu (which is merely a button) is clicked, the menu items are displayed as a JPopupMenu.

Images can be added to MenuItems. The constructor accepts an object of the ImageIcon class. Construct an ImageIcon with a gif, jpg or png image file, and pass it as a parameter to JMenuItem.

For those of you wishing to experiment with AWT API for menus, here is the sample program that does the same things as the program in listing 1, but uses the Frame class instead. It has no swing components.

listing 2

// MenuShortcut.java
/*
* The code shows how to set the menu bar for a Frame.
* The menu bar is set using the setMenuBar method.
* To add a Menu to a MenuBar, we use the add(Menu) method.
* To add menu items to a Menu, we use the add(MenuItem) method.
* Control key short cut launches the action associated with the shortcut
*/

// use awt components
import java.awt.*;

// use awt event listeners
import java.awt.event.*;

public class MenuShortcut extends Frame {

MenuBar menuBar;
Menu menu;
MenuItem menuItem;

public MenuShortcut() {

// Create & set the menu bar.
menuBar = new MenuBar();
setMenuBar(menuBar);

// Build the first menu.
menu = new Menu("File");
menuBar.add(menu);

/* Create some JMenuItems. MenuShortcut automatically assigns
Control key
*/

// create a menu item
menuItem = new MenuItem("New", new java.awt.MenuShortcut(KeyEvent.VK_N));
// add listener
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("Menu item 'New' clicked.");
}
});
// add to menu
menu.add(menuItem);

// add a separator
menu.addSeparator();

// create another menu item
menuItem = new MenuItem("Exit", new java.awt.MenuShortcut(KeyEvent.VK_X));
// add listener
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("Menu item 'Exit' clicked.");
System.exit(0);
}
});
// add to menu
menu.add(menuItem);

// add a window listener to the frame to enable closing
addWindowListener(new WindowAdapter() { // anonymous inner class
public void windowClosing(WindowEvent evt) {
// exit program
System.exit(0);
}
});

setTitle("MenuShortcut Example");
setSize(300,200);
setVisible(true);
}

// test drive the program
public static void main(String[] args) {

new MenuShortcut();
}
}

There is a slight difference in the use of shortcuts between the two programs. The swing version uses ALT+F+X to close the window, whereas the AWT version uses CTRL+X to do the same thing. I believe CTRL+X is reserved for cut-paste operations in swing, and may not be assignable as shortcuts for this reason. You may have noticed that the AWT version automatically assigns CONTROL key to the shortcuts specified. The swing version uses the ALT key by default.