Java Swing KeyStroke setActionKeyBinding(final JComponent component, final int condition, final KeyStroke keyStroke, final Action action)

Here you can find the source of setActionKeyBinding(final JComponent component, final int condition, final KeyStroke keyStroke, final Action action)

Description

Registers the given action for a keystroke.

License

Apache License

Parameter

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.

Return

The previous action key bound to the keystroke, if there is one.

Declaration

public static Object setActionKeyBinding(final JComponent component, final int condition,
        final KeyStroke keyStroke, final Action action) 

Method Source Code

//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);
    }
}

Related

  1. removeButtonClickKeystroke(AbstractButton b, KeyStroke key)
  2. removeKeyBinding(JComponent component, String key)
  3. removeKeyCode(JComponent comp, int keyCode)
  4. removeShortcut(JRootPane rootPane, String command, KeyStroke stroke)
  5. setAcceleratorKey(Action action, KeyStroke acceleratorKey)
  6. setGlobalAccelerator(JComponent component, KeyStroke accelerator, Action action)
  7. setHotKeyForFocus(final JComponent comp, final String keyStroke, final String actionName)
  8. setKeyBinding(String actionKey, int key, int modifiers, AbstractAction action, JComponent component)
  9. setKeyStroke(JComponent component, KeyStroke keyStroke, Action action)