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

voidrefleshAction(JComponent com, KeyStroke keyStroke)
reflesh Action
InputMap im = com.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
Object o = im.get(keyStroke);
if (o == null) {
    im = com.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    o = im.get(keyStroke);
if (o != null) {
    Action a = com.getActionMap().get(o);
...
voidregisterAction(JComponent comp, Action action, String key)
Register action with the input map.
KeyStroke ks = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
if (ks != null) {
    comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks, key);
    comp.getActionMap().put(key, action);
voidregisterAction(JComponent component, int condition, KeyStroke key, Action action, String command)
register Action
component.getInputMap(condition).put(key, command);
component.getActionMap().put(command, action);
voidregisterAsAction(final KeyStroke keyStroke, final String actionKey, final Runnable action, final JComponent component)
register As Action
final InputMap inputMap = component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(keyStroke, actionKey);
component.getActionMap().put(inputMap.get(keyStroke), new AbstractAction() {
    public void actionPerformed(final ActionEvent e) {
        action.run();
});
voidregisterKeyBinding(final JComponent aComponent, final String aKeyStroke, final Action aAction)
Registers a given keystroke to invoke a given action on the given component.
registerKeyBinding(aComponent, KeyStroke.getKeyStroke(aKeyStroke), aAction);
voidregisterKeyBoardAction(JComponent comp, Action action, KeyStroke stroke)
register Key Board Action
comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, action.getValue(Action.NAME));
comp.getActionMap().put(action.getValue(Action.NAME), action);
voidregisterKeyHandler(JComponent component, KeyStroke stroke, final ActionListener listener, final String actionCommand)
Registers a keyboard handler to a given component
component.getInputMap().put(stroke, actionCommand);
component.getActionMap().put(actionCommand, new AbstractAction() {
    private static final long serialVersionUID = 1L;
    public void actionPerformed(ActionEvent event) {
        ActionEvent newEvent = new ActionEvent(event.getSource(), ActionEvent.ACTION_PERFORMED,
                actionCommand);
        listener.actionPerformed(newEvent);
});
voidregisterKeystroke(final JComponent aComponent, final Action aAction, final String aCommandName)
Registers the keystroke of the given action as "command" of the given component.
final KeyStroke keyStroke = (KeyStroke) aAction.getValue(Action.ACCELERATOR_KEY);
if (keyStroke == null) {
    return;
InputMap inputMap = aComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = aComponent.getActionMap();
inputMap.put(keyStroke, aCommandName);
actionMap.put(aCommandName, aAction);
...
voidregisterTabKey(Container container)
register Tab Key
if ((container instanceof JComponent)) {
    ((JComponent) container).registerKeyboardAction(new AbstractAction()
        public void actionPerformed(ActionEvent e)
            DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
    , KeyStroke.getKeyStroke(9, 0), 0);
else
    for (int i = 0; i < container.getComponentCount(); i++) {
        Component c = container.getComponent(i);
        if (((c instanceof JComponent)) && (c.isFocusable()))
            ((JComponent) container).registerKeyboardAction(new AbstractAction()
                public void actionPerformed(ActionEvent e)
                    DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager()
                            .focusNextComponent();
            , KeyStroke.getKeyStroke(9, 0), 0);
voidremoveAcceleratorFromChildren(Container container, KeyStroke accelerator)
Removes the specified keystroke form the input mappings of all JComponent subcomponents of the specified container.
for (Component child : container.getComponents()) {
    if (child instanceof JComponent) {
        removeAcceleratorFromComponent((JComponent) child, accelerator);
    if (child instanceof Container) {
        removeAcceleratorFromChildren((Container) child, accelerator);