Java tutorial
//package com.java2s; import javax.swing.ComponentInputMap; import javax.swing.InputMap; import javax.swing.JComponent; import javax.swing.JMenuItem; import javax.swing.KeyStroke; public class Main { public static void addAccelerator(JMenuItem menuItem) { updateAccelerator(menuItem, null); } public static void updateAccelerator(JMenuItem menuItem, KeyStroke oldAccelerator) { KeyStroke accelerator = menuItem.getAccelerator(); if (oldAccelerator != null && oldAccelerator.equals(accelerator)) { return; } InputMap map = menuItem.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); if (map != null && oldAccelerator != null) { map.remove(oldAccelerator); } if (accelerator != null) { if (map == null) { map = new ComponentInputMap(menuItem); menuItem.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map); } map.put(accelerator, "click"); } } }