Java tutorial
//package com.java2s; import java.awt.event.KeyEvent; import javax.swing.ComponentInputMap; import javax.swing.InputMap; import javax.swing.JComponent; import javax.swing.KeyStroke; public class Main { public static void addMnemonic(JComponent c, int key) { updateMnemonic(c, 0, key); } public static void updateMnemonic(JComponent c, int oldKey, int newKey) { if (oldKey == newKey) { return; } InputMap map = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); if (map != null && oldKey != 0) { map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.ALT_MASK)); } if (newKey != 0) { if (map == null) { map = new ComponentInputMap(c); c.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map); } map.put(KeyStroke.getKeyStroke(newKey, KeyEvent.ALT_MASK), "press"); } } }