Java Swing KeyStroke addShortcutToComponent(JComponent component, KeyStroke keystroke, String actionCommand, Action action)

Here you can find the source of addShortcutToComponent(JComponent component, KeyStroke keystroke, String actionCommand, Action action)

Description

Adds the shortcut to component.

License

Apache License

Parameter

Parameter Description
component the component
keystroke the keystroke
actionCommand the action command
action the action

Declaration

public static void addShortcutToComponent(JComponent component, KeyStroke keystroke, String actionCommand,
        Action action) 

Method Source Code

//package com.java2s;
/**/*from  ww w .  ja  v a2  s .c o  m*/
 * Copyright (C) 2007 Asterios Raptis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.ComponentInputMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ActionMapUIResource;

public class Main {
    /**
     * Adds the shortcut to component.
     *
     * @param component
     *            the component
     * @param keystroke
     *            the keystroke
     * @param actionCommand
     *            the action command
     * @param action
     *            the action
     */
    public static void addShortcutToComponent(JComponent component, KeyStroke keystroke, String actionCommand,
            Action action) {
        addShortcutToComponent(component, keystroke, JComponent.WHEN_IN_FOCUSED_WINDOW, actionCommand, action);
    }

    /**
     * Adds the shortcut to component.
     *
     * @param component
     *            the component
     * @param keystroke
     *            the keystroke
     * @param whenInFocus
     *            the whe in focus
     * @param actionCommand
     *            the action command
     * @param action
     *            the action
     */
    public static void addShortcutToComponent(JComponent component, KeyStroke keystroke, int whenInFocus,
            String actionCommand, Action action) {
        InputMap keyMap = new ComponentInputMap(component);
        keyMap.put(keystroke, actionCommand);
        ActionMap actionMap = new ActionMapUIResource();
        actionMap.put(actionCommand, action);
        SwingUtilities.replaceUIActionMap(component, actionMap);
        SwingUtilities.replaceUIInputMap(component, whenInFocus, keyMap);
    }
}

Related

  1. addKeyboardAction(Action action, KeyStroke keyStroke, JComponent component)
  2. addKeyboardShortcut(final JComponent target, final AbstractButton button, final KeyStroke keyStroke)
  3. addShortcut(JRootPane rootPane, String command, Action action, KeyStroke stroke)
  4. addShortcutAction(String name, JComponent component, KeyStroke key, Action action)
  5. addShortcutToComponent(final JComponent component, final KeyStroke keystroke, final String actionCommand, final Action action)
  6. bindAction(JComponent aComponent, String aCommand, Action anAction, KeyStroke aKeyStroke)
  7. bindKeyToAction(int keyCode, int modifiers, Action action, JComponent component, int condition)
  8. bindKeyToAction(JComponent c, KeyStroke key, Action a)
  9. clearActionBinding(final JComponent component, final KeyStroke keyStroke, final int condition)