Here you can find the source of registerKeyBinding(final JComponent aComponent, final String aKeyStroke, final Action aAction)
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. |
public static void registerKeyBinding(final JComponent aComponent, final String aKeyStroke, final Action aAction)
//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); } }