Hotkeys

Learn to set menu and button hotkeys in Java

Q: How do you set menu and button hotkeys in Java? I use JDK 1.1.4; is the method to do this in JDK 1.2 any different?

A:

It'll work with both JDK 1.1 and JDK 1.2. Please note that with JDK 1.1, you'll need to use a Swing version more recent than 1.0.2.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ShortCutKeys extends JApplet
{
   JButton button;
   JCheckBox checkBox;
   JMenuBar menuBar;
   JMenu fileMenu;
   JMenuItem exitMenuItem;
   JPanel panel;
 public void init()
 {
    Container container = this.getContentPane();
 
    Handler eventHandler = new Handler();
 
    checkBox = new JCheckBox("Hello Mom!");
    checkBox.setMnemonic(java.awt.event.KeyEvent.VK_M);
       checkBox.addActionListener(eventHandler);
    button = new JButton("Hello Dad!");
    button.setMnemonic(java.awt.event.KeyEvent.VK_D);
       button.addActionListener(eventHandler);
    panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.add(checkBox);
    panel.add(button);
       exitMenuItem = new JMenuItem("Exit");
       exitMenuItem.setMnemonic('x');
       exitMenuItem.addActionListener(eventHandler);
    fileMenu = new JMenu("File");
    fileMenu.setMnemonic('f');
    fileMenu.add(exitMenuItem);
    menuBar = new JMenuBar();
    menuBar.add(fileMenu);
    container.add(menuBar, BorderLayout.NORTH);
  container.add(panel, BorderLayout.CENTER);
 }
 class Handler implements ActionListener
 {
    public void actionPerformed(ActionEvent ae)
    {
       if (ae.getSource() == checkBox)
       {
          System.err.println("Action Performed on CHECKBOX");
       }
       else if (ae.getSource() == button)
       {
          System.err.println("Action Performed on BUTTON");
       }
       else if (ae.getSource() == exitMenuItem)
       {
          System.exit(0);
       }
    }
 }
    
 public static void main(String[] args)
 {
    JFrame frame = new JFrame("Short Cut Keys");
    ShortCutKeys sck = new ShortCutKeys();
    sck.init();
    frame.getContentPane().add(sck);
    frame.addWindowListener(new WindowAdapter()
    {
       public void windowClosing(WindowEvent we)
       { System.exit(0); }
       }
       );
       frame.setSize(300, 100);
       frame.show();
    }
}
Random Walk Computing is the largest Java/CORBA consulting boutique in New York, focusing on solutions for the financial enterprise. Known for their leading-edge Java expertise, Random Walk consultants publish and speak about Java in some of the most respected forums in the world.
Related: