Java tutorial
//package com.java2s; import javax.swing.Action; import javax.swing.JComponent; import javax.swing.KeyStroke; public class Main { public static void registerKeyBoardAction(JComponent comp, Action action) { registerKeyBoardAction(comp, action, JComponent.WHEN_IN_FOCUSED_WINDOW); } public static void registerKeyBoardAction(JComponent comp, Action action, KeyStroke stroke) { comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, action.getValue(Action.NAME)); comp.getActionMap().put(action.getValue(Action.NAME), action); } /** * * @param comp * @param action * @param condition - see {@link JComponent} * (WHEN_FOCUSED, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,WHEN_IN_FOCUSED_WINDOW) */ public static void registerKeyBoardAction(JComponent comp, Action action, int condition) { comp.getInputMap(condition).put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), action.getValue(Action.NAME)); comp.getActionMap().put(action.getValue(Action.NAME), action); } }