Java tutorial
//package com.java2s; import javax.swing.Action; import javax.swing.JComponent; import javax.swing.KeyStroke; public class Main { public static void setKeyAction_WhenFocused(JComponent component, int keyCode, String actionIdString, Action action) { component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(keyCode, 0), actionIdString); component.getActionMap().put(actionIdString, action); } }