Java tutorial
//package com.java2s; /* * Copyright (C) 2009 Klaus Reimer <k@ailis.de> * See LICENSE.md for licensing information. */ import javax.swing.Action; import javax.swing.ActionMap; import javax.swing.InputMap; import javax.swing.JComponent; import javax.swing.KeyStroke; public class Main { /** * Adds a component action. * * @param component * The compoennt to add the action to * @param action * The action to add */ public static void addComponentAction(final JComponent component, final Action action) { final InputMap imap = component .getInputMap(component.isFocusable() ? JComponent.WHEN_FOCUSED : JComponent.WHEN_IN_FOCUSED_WINDOW); final ActionMap amap = component.getActionMap(); final KeyStroke ks = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY); imap.put(ks, action.getValue(Action.NAME)); amap.put(action.getValue(Action.NAME), action); } }