Here you can find the source of addShortcutToComponent(JComponent component, KeyStroke keystroke, String actionCommand, Action action)
Parameter | Description |
---|---|
component | the component |
keystroke | the keystroke |
actionCommand | the action command |
action | the action |
public static void addShortcutToComponent(JComponent component, KeyStroke keystroke, String actionCommand, Action action)
//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); } }