Java tutorial
//package com.java2s; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.AbstractAction; import javax.swing.AbstractButton; import javax.swing.Action; import javax.swing.ActionMap; import javax.swing.InputMap; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.KeyStroke; public class Main { public static void associaTeclaAtalho(JComponent component, Action action, String nomeAcao, String... atalhos) { final InputMap inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); final ActionMap actionMap = component.getActionMap(); for (String atalho : atalhos) { KeyStroke keyStroke = KeyStroke.getKeyStroke(atalho); inputMap.put(keyStroke, nomeAcao); actionMap.put(nomeAcao, action); } } public static void associaTeclaAtalho(JFrame frame, Action action, String nomeAcao, String... atalhos) { associaTeclaAtalho(frame.getRootPane(), action, nomeAcao, atalhos); } public static void associaTeclaAtalho(JDialog dialog, Action action, String nomeAcao, String... atalhos) { associaTeclaAtalho(dialog.getRootPane(), action, nomeAcao, atalhos); } public static void associaTeclaAtalho(final AbstractButton button, String nomeAcao, String... atalhos) { Action action = new AbstractAction(nomeAcao) { @Override public void actionPerformed(ActionEvent e) { for (ActionListener listener : button.getActionListeners()) { listener.actionPerformed(e); } } }; associaTeclaAtalho(button, action, nomeAcao, atalhos); } }