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

voidremoveAcceleratorFromComponent(JComponent component, KeyStroke accelerator)
Removes the specified key mapping from a component.
removeAcceleratorFromMap(component.getInputMap(JComponent.WHEN_FOCUSED), accelerator);
removeAcceleratorFromMap(component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT), accelerator);
removeAcceleratorFromMap(component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW), accelerator);
voidremoveAcceleratorFromMap(InputMap map, KeyStroke accelerator)
Removes a keystroke from an input map and its parent map.
map.remove(accelerator);
InputMap parent = map.getParent();
if (parent != null) {
    parent.remove(accelerator);
voidremoveButtonClickKeystroke(AbstractButton b, KeyStroke key)
The reverse Operation of #addButtonClickKeystroke(AbstractButton,KeyStroke,Object) .
InputMap inMap = b.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap acMap = b.getActionMap();
acMap.remove(inMap.get(key));
inMap.remove(key);
voidremoveKeyBinding(JComponent component, String key)
remove Key Binding
int condition = JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
InputMap inputMap = component.getInputMap(condition);
inputMap.put(KeyStroke.getKeyStroke(key), new Object());
voidremoveKeyCode(JComponent comp, int keyCode)
remove Key Code
removeKeyCode(comp.getInputMap(JComponent.WHEN_FOCUSED), keyCode);
removeKeyCode(comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT), keyCode);
removeKeyCode(comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW), keyCode);
voidremoveShortcut(JRootPane rootPane, String command, KeyStroke stroke)
remove Shortcut
InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.remove(stroke);
rootPane.getActionMap().remove(command);
voidsetAcceleratorKey(Action action, KeyStroke acceleratorKey)
Put the accelerator key value.
action.putValue(Action.ACCELERATOR_KEY, acceleratorKey);
ObjectsetActionKeyBinding(final JComponent component, final int condition, final KeyStroke keyStroke, final Action action)
Registers the given action for a keystroke.
if (component == null)
    throw new IllegalArgumentException("Parameter 'component' must not be null!");
if (keyStroke == null)
    throw new IllegalArgumentException("Parameter 'keyStroke' must not be null!");
if (action == null)
    throw new IllegalArgumentException("Parameter 'action' must not be null!");
String actionKey = (String) action.getValue(Action.NAME);
if (actionKey != null)
...
voidsetGlobalAccelerator(JComponent component, KeyStroke accelerator, Action action)
Sets an accelerator for all subcomponents of the specified JComponent .
Object actionCommand = action.getValue(Action.ACTION_COMMAND_KEY);
if (actionCommand == null) {
    actionCommand = action.getValue(Action.NAME);
component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(accelerator, actionCommand);
component.getActionMap().put(actionCommand, action);
removeAcceleratorFromChildren(component, accelerator);
voidsetHotKeyForFocus(final JComponent comp, final String keyStroke, final String actionName)
Sets the hot key for focus.
final ActionMap amap = comp.getActionMap();
amap.put(actionName, new AbstractAction() {
    private static final long serialVersionUID = 1L;
    @Override
    public void actionPerformed(final ActionEvent e) {
        comp.requestFocus();
});
...