Java Swing Key Action registerKeyAction(JComponent component, int keyCode, Action action)

Here you can find the source of registerKeyAction(JComponent component, int keyCode, Action action)

Description

A simplified version of another method of the same name for registering a handler for certain key events.

License

Apache License

Parameter

Parameter Description
component The component against which the key action is registered.
keyCode The key code to register, as specified in the KeyEvent class's VK constants.
action The action to perform when the key stroke is received.

Declaration

public static void registerKeyAction(JComponent component, int keyCode, Action action) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.swing.*;

public class Main {
    /**//from   w ww . j a v a2s  .  c o m
     * Registers an action against a key stroke for a particular component. Must
     * be invoked from EDT.
     *
     * @param component The component against which the key action is
     * registered.
     * @param when The condition when this action is active (see
     * JComponent.WHEN...)
     * @param key The key stroke to register.
     * @param action The action to perform when the key stroke is received.
     */
    public static void registerKeyAction(JComponent component, int when, KeyStroke key, Action action) {
        component.getInputMap(when).put(key, key);
        component.getActionMap().put(key, action);
    }

    /**
     * A simplified version of another method of the same name for registering a
     * handler for certain key events. Must be invoked from EDT.
     *
     * @param component The component against which the key action is
     * registered.
     * @param keyCode The key code to register, as specified in the KeyEvent
     * class's VK constants.
     * @param action The action to perform when the key stroke is received.
     */
    public static void registerKeyAction(JComponent component, int keyCode, Action action) {
        registerKeyAction(component, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
                KeyStroke.getKeyStroke(keyCode, 0), action);
    }

    /**
     * A simplified version of another method of the same name for registering a
     * handler for certain key events. Must be invoked from EDT.
     *
     * @param component The component against which the key action is
     * registered.
     * @param keyCode The key code to register, as specified in the KeyEvent
     * class's VK constants.
     * @param modifiers Combination of zero or more modifiers from
     * InputEvent.<i>XXX</i>_DOWN_MASK
     * @param action The action to perform when the key stroke is received.
     */
    public static void registerKeyAction(JComponent component, int keyCode, int modifiers, Action action) {
        registerKeyAction(component, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
                KeyStroke.getKeyStroke(keyCode, modifiers), action);
    }
}

Related

  1. matches(Action action, KeyEvent ke)
  2. parseKeyStroke(String keyStroke)
  3. parseKeyStroke(String keyStroke)
  4. pressKey(Component component, int keyCode, int modifier)
  5. registerKey(JComponent component, final int keyEvent, int modifiers, Action action)
  6. registerTabKey(Container container)
  7. registerWindowCloseKeys(JRootPane root, Action closeAction)
  8. removeTabbedPaneFocusTraversalKeyBindings( JComponent c)
  9. replaceAction(InputMap map, char c)