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