Java tutorial
//package com.java2s; import javax.swing.Action; import javax.swing.JComponent; import javax.swing.KeyStroke; public class Main { /** * Registers an action for a key-stroke in a component. * @param actionName The name for the action related to the keystroke. If null, the keystroke description is used. * @param focusType e.g. {@link JComponent#WHEN_FOCUSED} * @param ks The keystroke that activates the action. */ public static void setKeyAction(JComponent component, Action action, String actionName, int focusType, KeyStroke ks) { if (actionName == null) { actionName = ks.toString(); } component.getInputMap(focusType).put(ks, actionName); component.getActionMap().put(actionName, action); } }