Java Swing KeyStroke installKeystrokes(JComponent component, Action... actions)

Here you can find the source of installKeystrokes(JComponent component, Action... actions)

Description

Install the keystrokes for several actions on a single button.

License

Open Source License

Parameter

Parameter Description
component The component to which the key stroke will be added.
actions The actions to add to the keystrokes.

Declaration

public static void installKeystrokes(JComponent component, Action... actions) 

Method Source Code

//package com.java2s;
/*//from   w w  w .  j av  a2 s  .  c  om
 *  Jajuk
 *  Copyright (C) The Jajuk Team
 *  http://jajuk.info
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *  
 */

import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

public class Main {
    /**
     * Install the keystrokes for several actions on a single button. The
     * keystrokes are added with {@link JComponent#WHEN_IN_FOCUSED_WINDOW}
     * condition.
     * 
     * @param component The component to which the key stroke will be added.
     * @param actions The actions to add to the keystrokes.
     */
    public static void installKeystrokes(JComponent component, Action... actions) {
        for (Action action : actions) {
            KeyStroke stroke = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
            if (stroke != null) {
                InputMap keyMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
                keyMap.put(stroke, action);
                ActionMap actionMap = component.getActionMap();
                actionMap.put(action, action);
            }
        }
    }
}

Related

  1. getMnemonic(final String key)
  2. installAction(JComponent comp, Action action, KeyStroke keyStroke, String actionKey)
  3. installKey(final JComponent comp, final String keyString, final int key, final Action action)
  4. installKeyBinding(JComponent c, int condition, String actionName, Action action, KeyStroke... keyStrokes)
  5. installKeyBindings()
  6. isKeyStroke(String propClass)
  7. mapInput(JComponent component, int scope, final int keycode, final int modifiers, final AbstractAction action)
  8. mapKeyStrokeAction(JComponent component, String actionMapKey, Action action, KeyStroke keyStroke)
  9. mapKeyStrokeToAction(final JComponent comp, final String ks, final String name, final AbstractAction action)