Java Swing KeyStroke registerKeyBinding(final JComponent aComponent, final String aKeyStroke, final Action aAction)

Here you can find the source of registerKeyBinding(final JComponent aComponent, final String aKeyStroke, final Action aAction)

Description

Registers a given keystroke to invoke a given action on the given component.

License

Open Source License

Parameter

Parameter Description
aComponent the component to register the keystroke for;
aKeyStroke the keystroke (as String) to register;
aAction the action to invoke when the keystroke is typed.

Declaration

public static void registerKeyBinding(final JComponent aComponent, final String aKeyStroke,
        final Action aAction) 

Method Source Code

//package com.java2s;
/*/*from   ww  w  .j av  a 2 s . c o  m*/
 * OpenBench LogicSniffer / SUMP project
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * 
 * Copyright (C) 2010-2011 - J.W. Janssen, http://www.lxtreme.nl
 */

import javax.swing.*;

public class Main {
    /**
     * Registers a given keystroke to invoke a given action on the given
     * component.
     * 
     * @param aComponent
     *          the component to register the keystroke for;
     * @param aKeyStroke
     *          the keystroke (as plain char) to register;
     * @param aAction
     *          the action to invoke when the keystroke is typed.
     */
    public static void registerKeyBinding(final JComponent aComponent, final char aKey, final Action aAction) {
        registerKeyBinding(aComponent, KeyStroke.getKeyStroke(aKey), aAction);
    }

    /**
     * Registers a given keystroke to invoke a given action on the given
     * component.
     * 
     * @param aComponent
     *          the component to register the keystroke for;
     * @param aKeyStroke
     *          the keystroke to register;
     * @param aAction
     *          the action to invoke when the keystroke is typed.
     */
    public static void registerKeyBinding(final JComponent aComponent, final KeyStroke aKeyStroke,
            final Action aAction) {
        final String name = "KeyBinding.".concat(aKeyStroke.toString());
        aComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(aKeyStroke, name);
        aComponent.getActionMap().put(name, aAction);
    }

    /**
     * Registers a given keystroke to invoke a given action on the given
     * component.
     * 
     * @param aComponent
     *          the component to register the keystroke for;
     * @param aKeyStroke
     *          the keystroke (as String) to register;
     * @param aAction
     *          the action to invoke when the keystroke is typed.
     */
    public static void registerKeyBinding(final JComponent aComponent, final String aKeyStroke,
            final Action aAction) {
        registerKeyBinding(aComponent, KeyStroke.getKeyStroke(aKeyStroke), aAction);
    }
}

Related

  1. maybeInstall(InputMap map, String action, KeyStroke stroke)
  2. refleshAction(JComponent com, KeyStroke keyStroke)
  3. registerAction(JComponent comp, Action action, String key)
  4. registerAction(JComponent component, int condition, KeyStroke key, Action action, String command)
  5. registerAsAction(final KeyStroke keyStroke, final String actionKey, final Runnable action, final JComponent component)
  6. registerKeyBoardAction(JComponent comp, Action action, KeyStroke stroke)
  7. registerKeyHandler(JComponent component, KeyStroke stroke, final ActionListener listener, final String actionCommand)
  8. registerKeystroke(final JComponent aComponent, final Action aAction, final String aCommandName)
  9. registerTabKey(Container container)