Here you can find the source of setActionKeyBinding(final JComponent component, final int condition, final KeyStroke keyStroke, final Action action)
Parameter | Description |
---|---|
component | Compontent the action should be registered on. |
condition | The condition for the input map (as you would pass to JComponent#getInputMap(int)) |
keyStroke | Keystroke the action should be registered for. |
action | Action to register for the given keystroke. |
public static Object setActionKeyBinding(final JComponent component, final int condition, final KeyStroke keyStroke, final Action action)
//package com.java2s; /**//from w w w . j a v a2 s .c o m * todo [heup] add docs <hr/> Copyright 2006-2012 Torsten Heup * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ import javax.swing.*; public class Main { /** * Registers the given action for a keystroke. * * @param component Compontent the action should be registered on. * @param condition The condition for the input map (as you would pass to JComponent#getInputMap(int)) * @param keyStroke Keystroke the action should be registered for. * @param action Action to register for the given keystroke. * @return The previous action key bound to the keystroke, if there is one. */ public static Object setActionKeyBinding(final JComponent component, final int condition, final KeyStroke keyStroke, final Action action) { if (component == null) throw new IllegalArgumentException("Parameter 'component' must not be null!"); if (keyStroke == null) throw new IllegalArgumentException("Parameter 'keyStroke' must not be null!"); if (action == null) throw new IllegalArgumentException("Parameter 'action' must not be null!"); String actionKey = (String) action.getValue(Action.NAME); if (actionKey != null) actionKey = "action" + System.identityHashCode(action); final InputMap map = component.getInputMap(condition); final Object previousKeyBinding = map.get(keyStroke); map.put(keyStroke, actionKey); component.setInputMap(condition, map); component.getActionMap().put(actionKey, action); return previousKeyBinding; } /** * Registers the given action for a keystroke, using JComponent.WHEN_FOCUSED as condition for the input map. * * @param component Compontent the action should be registered on. * @param keyStroke Keystroke the action should be registered for. * @param action Action to register for the given keystroke. * @return The previous action key bound to the keystroke, if there is one. */ public static Object setActionKeyBinding(final JComponent component, final KeyStroke keyStroke, final Action action) { return setActionKeyBinding(component, JComponent.WHEN_FOCUSED, keyStroke, action); } }