Java Swing KeyStroke addHotKey(JComponent pane, Action action, KeyStroke key)

Here you can find the source of addHotKey(JComponent pane, Action action, KeyStroke key)

Description

Add a quick keystroke on the given pane for the given action.

License

Open Source License

Declaration

public static void addHotKey(JComponent pane, Action action,
        KeyStroke key) 

Method Source Code

//package com.java2s;
/*/* www .j a v a2s .  c  o m*/
 Copyright (c) 1998-2013 The Regents of the University of California
 All rights reserved.
 Permission is hereby granted, without written agreement and without
 license or royalty fees, to use, copy, modify, and distribute this
 software and its documentation for any purpose, provided that the above
 copyright notice and the following two paragraphs appear in all copies
 of this software.

 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 PROVIDED HEREUNDER IS ON AN  BASIS, AND THE UNIVERSITY OF
 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 ENHANCEMENTS, OR MODIFICATIONS.

 PT_COPYRIGHT_VERSION_2
 COPYRIGHTENDKEY
 */

import javax.swing.Action;

import javax.swing.JComponent;

import javax.swing.KeyStroke;

public class Main {
    /** JDK1.2 doesn't have this string defined in javax.swing.Action.
     *  This is the value that JDK1.3 uses.
     */
    public static final String ACCELERATOR_KEY = "AcceleratorKey";

    /** Add a quick keystroke on the given pane for the given action.
     *  The keystroke that is added is given in the ACCELERATOR_KEY
     *  property that has been set in the action.  If the ACCELERATOR_KEY
     *  property has not been set, then do not add a hotkey.
     */
    public static void addHotKey(JComponent pane, Action action) {
        addHotKey(pane, action, null);
    }

    /** Add a quick keystroke on the given pane for the given action.
     *  If the given keystroke is null, then use the ACCELERATOR_KEY
     *  property that has been set in the action.  If the given keystroke
     *  is null, Otherwise, set the
     *  ACCELERATOR_KEY property to the given key stroke.
     */
    public static void addHotKey(JComponent pane, Action action,
            KeyStroke key) {
        String name = (String) action.getValue(Action.NAME);

        if (key == null) {
            key = (KeyStroke) action.getValue(ACCELERATOR_KEY);
        } else {
            action.putValue(ACCELERATOR_KEY, key);
        }

        if (key != null) {
            pane.registerKeyboardAction(action, name, key,
                    JComponent.WHEN_IN_FOCUSED_WINDOW);
        }
    }
}

Related

  1. addButtonClickKeystroke(final AbstractButton b, KeyStroke ks, Object key)
  2. addFocusBackKey(final int button)
  3. addFocusTraversalKey(final JComponent target, final int keyCode, final int modifiers, final int id)
  4. addHotKey(JComponent pane, Action action, KeyStroke key)
  5. addKeyBinding(JComponent comp, KeyStroke key, String id, Action action)
  6. addKeyboardAction(Action action, KeyStroke keyStroke, JComponent component)
  7. addKeyboardShortcut(final JComponent target, final AbstractButton button, final KeyStroke keyStroke)