Example usage for javax.swing Action ACCELERATOR_KEY

List of usage examples for javax.swing Action ACCELERATOR_KEY

Introduction

In this page you can find the example usage for javax.swing Action ACCELERATOR_KEY.

Prototype

String ACCELERATOR_KEY

To view the source code for javax.swing Action ACCELERATOR_KEY.

Click Source Link

Document

The key used for storing a KeyStroke to be used as the accelerator for the action.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    final Action action = new AbstractAction("Action Name") {
        {//from  w w w . j av a  2  s .  c o  m
            putValue(Action.SHORT_DESCRIPTION, "Tool Tip Text");

            putValue(Action.LONG_DESCRIPTION, "Help Text");

            Icon icon = new ImageIcon("icon.gif");
            putValue(Action.SMALL_ICON, icon);

            putValue(Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_A));
            putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control F2"));
        }

        public void actionPerformed(ActionEvent evt) {
            System.out.println("action");
        }
    };

    JButton button = new JButton(action);
}

From source file:Main.java

/**
 * Binds an action to a component. Uses the <code>Action.ACTION_COMMAND_KEY</code> and <code>Action.ACCELERATOR_KEY</code> action
 * properties.//from   ww  w . j  ava 2 s.  co  m
 * 
 * @param component
 *            the component.
 * @param action
 *            the action to bind.
 */
public static void bindAction(JComponent component, Action action) {
    component.getInputMap().put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY),
            action.getValue(Action.ACTION_COMMAND_KEY));
    component.getActionMap().put(action.getValue(Action.ACTION_COMMAND_KEY), action);
}

From source file:Main.java

/**
 * Adds a component action./*from  w  ww. j a v a 2s.  co m*/
 *
 * @param component
 *            The compoennt to add the action to
 * @param action
 *            The action to add
 */

public static void addComponentAction(final JComponent component, final Action action) {
    final InputMap imap = component
            .getInputMap(component.isFocusable() ? JComponent.WHEN_FOCUSED : JComponent.WHEN_IN_FOCUSED_WINDOW);
    final ActionMap amap = component.getActionMap();
    final KeyStroke ks = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
    imap.put(ks, action.getValue(Action.NAME));
    amap.put(action.getValue(Action.NAME), action);
}

From source file:Main.java

/**
 * /*w w w.  ja v a  2  s .  c  o  m*/
 * @param comp
 * @param action
 * @param condition - see {@link JComponent}
 * (WHEN_FOCUSED, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,WHEN_IN_FOCUSED_WINDOW)
 */
public static void registerKeyBoardAction(JComponent comp, Action action, int condition) {
    comp.getInputMap(condition).put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY),
            action.getValue(Action.NAME));
    comp.getActionMap().put(action.getValue(Action.NAME), action);
}

From source file:Main.java

public static void bindAction(JComponent aComponent, String aCommand, Action anAction) {
    Object o = anAction.getValue(Action.ACCELERATOR_KEY);
    if (o instanceof KeyStroke)
        bindAction(aComponent, aCommand, anAction, (KeyStroke) o);
    else/*from   w  w w .  j ava  2s .  co m*/
        throw new IllegalArgumentException("Can not bind action, it has no keystroke assigned.");
}

From source file:Main.java

public static void installActions(JComponent comp, Action actions[], int condition) {
    ActionMap actionMap = comp.getActionMap();
    InputMap inputMap = comp.getInputMap(condition);
    for (int i = 0; i < actions.length; i++) {
        String name = (String) actions[i].getValue(Action.NAME);
        actionMap.put(name, actions[i]);
        inputMap.put((KeyStroke) actions[i].getValue(Action.ACCELERATOR_KEY), name);
    }/*from  w  w  w  . j  a  v  a2s . com*/
}

From source file:Main.java

public ShowAction() {
    super("About");

    putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("A"));
    putValue(Action.NAME, "Go to number ");

}

From source file:Main.java

/**
 * Registers the keystroke of the given action as "command" of the given
 * component./*from w ww .jav  a  2s. c  om*/
 * <p>
 * This code is based on the Sulky-tools, found at
 * &lt;http://github.com/huxi/sulky&gt;.
 * </p>
 * 
 * @param aComponent
 *          the component that should react on the keystroke, cannot be
 *          <code>null</code>;
 * @param aAction
 *          the action of the keystroke, cannot be <code>null</code>;
 * @param aCommandName
 *          the name of the command to register the keystore under.
 */
public static void registerKeystroke(final JComponent aComponent, final Action aAction,
        final String aCommandName) {
    final KeyStroke keyStroke = (KeyStroke) aAction.getValue(Action.ACCELERATOR_KEY);
    if (keyStroke == null) {
        return;
    }

    InputMap inputMap = aComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = aComponent.getActionMap();
    inputMap.put(keyStroke, aCommandName);
    actionMap.put(aCommandName, aAction);

    inputMap = aComponent.getInputMap(JComponent.WHEN_FOCUSED);
    Object value = inputMap.get(keyStroke);
    if (value != null) {
        inputMap.put(keyStroke, aCommandName);
    }

    inputMap = aComponent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    value = inputMap.get(keyStroke);
    if (value != null) {
        inputMap.put(keyStroke, aCommandName);
    }
}

From source file:com.eviware.soapui.support.xml.actions.InsertBase64FileTextAreaAction.java

public InsertBase64FileTextAreaAction(RSyntaxTextArea editArea, String dialogTitle) {
    super("Insert file as Base64");

    this.textArea = editArea;
    this.dialogTitle = dialogTitle;
    if (UISupport.isMac()) {
        putValue(Action.ACCELERATOR_KEY, UISupport.getKeyStroke("meta G"));
    } else {//from   w w w . j av a  2  s .c  om
        putValue(Action.ACCELERATOR_KEY, UISupport.getKeyStroke("ctrl G"));
    }
}

From source file:net.sf.jabref.gui.help.HelpAction.java

public HelpAction(String title, String tooltip, HelpFile helpPage, KeyStroke key) {
    this(title, tooltip, helpPage, IconTheme.JabRefIcon.HELP.getSmallIcon());
    putValue(Action.ACCELERATOR_KEY, key);
}