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

voidaddButtonClickKeystroke(final AbstractButton b, KeyStroke ks, Object key)
Adds a KeyStroke to a AbstractButton .
b.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks, key);
b.getActionMap().put(key, new AbstractAction() {
    private static final long serialVersionUID = 2702589216282710389L;
    @Override
    public void actionPerformed(ActionEvent e) {
        b.doClick();
});
...
voidaddFocusBackKey(final int button)
Adds the focus back key.
final KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
final Set<?> oldBackKeys = manager
        .getDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
final Set<KeyStroke> backwordKeys = new HashSet(oldBackKeys);
backwordKeys.add(KeyStroke.getKeyStroke(button, 0));
manager.setDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backwordKeys);
voidaddFocusTraversalKey(final JComponent target, final int keyCode, final int modifiers, final int id)
add Focus Traversal Key
addFocusTraversalKey(target, KeyStroke.getAWTKeyStroke(keyCode, modifiers), id);
voidaddHotKey(JComponent pane, Action action, KeyStroke key)
Add a quick keystroke on the given pane for the given action.
String name = (String) action.getValue(Action.NAME);
if (key == null) {
    key = (KeyStroke) action.getValue(ACCELERATOR_KEY);
} else {
    action.putValue(ACCELERATOR_KEY, key);
if (key != null) {
    pane.registerKeyboardAction(action, name, key, JComponent.WHEN_IN_FOCUSED_WINDOW);
...
voidaddHotKey(JComponent pane, Action action, KeyStroke key)
Add a quick keystroke on the given pane for the given action.
String name = (String) action.getValue(action.NAME);
if (key == null) {
    key = (KeyStroke) action.getValue(ACCELERATOR_KEY);
} else {
    action.putValue(ACCELERATOR_KEY, key);
if (key != null)
    pane.registerKeyboardAction(action, name, key, pane.WHEN_IN_FOCUSED_WINDOW);
...
voidaddKeyBinding(JComponent comp, KeyStroke key, String id, Action action)
add Key Binding
comp.getInputMap().put(key, id);
comp.getActionMap().put(id, action);
voidaddKeyboardAction(Action action, KeyStroke keyStroke, JComponent component)
add Keyboard Action
String name = (String) action.getValue(Action.NAME);
component.getActionMap().put(name, action);
component.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke, name);
voidaddKeyboardShortcut(final JComponent target, final AbstractButton button, final KeyStroke keyStroke)
add Keyboard Shortcut
target.registerKeyboardAction(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (button.isEnabled()) {
            button.doClick();
}, keyStroke, JComponent.WHEN_FOCUSED);
...
voidaddShortcut(JRootPane rootPane, String command, Action action, KeyStroke stroke)
add Shortcut
InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(stroke, command);
rootPane.getActionMap().put(command, action);
voidaddShortcutAction(String name, JComponent component, KeyStroke key, Action action)
Add shortcut action to any JComponent.
ActionMap actionMap = component.getActionMap();
InputMap inputMap = component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(key, name);
actionMap.put(name, action);