Java Utililty Methods Swing KeyStroke

List of utility methods to do Swing KeyStroke

Description

The list of methods to do Swing KeyStroke are organized into topic(s).

Method

voidaddShortcutToComponent(final JComponent component, final KeyStroke keystroke, final String actionCommand, final Action action)
Adds the shortcut to component.
addShortcutToComponent(component, keystroke, JComponent.WHEN_IN_FOCUSED_WINDOW, actionCommand, action);
voidaddShortcutToComponent(JComponent component, KeyStroke keystroke, String actionCommand, Action action)
Adds the shortcut to component.
addShortcutToComponent(component, keystroke, JComponent.WHEN_IN_FOCUSED_WINDOW, actionCommand, action);
voidbindAction(JComponent aComponent, String aCommand, Action anAction, KeyStroke aKeyStroke)
bind Action
aComponent.getInputMap(JComponent.WHEN_FOCUSED).put(aKeyStroke, aCommand);
aComponent.getActionMap().put(aCommand, anAction);
voidbindKeyToAction(int keyCode, int modifiers, Action action, JComponent component, int condition)
bind Key To Action
KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, modifiers);
component.getInputMap(condition).put(keyStroke, action);
voidbindKeyToAction(JComponent c, KeyStroke key, Action a)
Convenience wrapper for #bindKeyToAction(JComponent,KeyStroke,Action,int) bindKeyToAction(c, key, a, JComponentn.WHEN_FOCUSED) .
bindKeyToAction(c, key, a, JComponent.WHEN_FOCUSED);
voidclearActionBinding(final JComponent component, final KeyStroke keyStroke, final int condition)
Clears the action key registered under the given keystroke.
InputMap map = component.getInputMap(condition);
while (map != null) {
    map.remove(keyStroke);
    map = map.getParent();
voidclearKeyStroke(JComponent component)
clear Key Stroke
InputMap inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
if (inputMap != null) {
    KeyStroke[] keyStrokes = inputMap.keys();
    if (keyStrokes != null) {
        for (KeyStroke keyStroke : inputMap.keys()) {
            if (KEYID.equals(inputMap.get(keyStroke))) {
                inputMap.remove(keyStroke);
                break;
...
voidcloseOnKeyStroke(JRootPane rootPane, KeyStroke keyStroke)
close On Key Stroke
final String CLOSE_ACTION_NAME = "com.jakeapp.gui.swing.helpers.GuiUtilities.CloseFrameOnKeyStroke";
rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, CLOSE_ACTION_NAME);
rootPane.getActionMap().put(CLOSE_ACTION_NAME, CLOSE_ACTION);
booleancomponentListensForKey(JComponent component, int keyCode)
Returns whether a component listens for a key code, i.e.
KeyStroke[] keyStrokes = component.getInputMap().allKeys();
if (keyStrokes == null)
    return false;
for (int i = keyStrokes.length - 1; i >= 0; i--) {
    if (keyCode == keyStrokes[i].getKeyCode()) {
        return true;
return false;
voidconfigAction(Action action, String text, Icon icon, KeyStroke keyStroke)
config Action
action.putValue(NAME, text);
action.putValue(SMALL_ICON, icon);
action.putValue(ACCELERATOR_KEY, keyStroke);