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;

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.
     *///from  w  w  w .j a v  a2  s  .  c o  m
    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, pane.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)
  8. addShortcut(JRootPane rootPane, String command, Action action, KeyStroke stroke)