Example usage for javax.swing JComponent getActionMap

List of usage examples for javax.swing JComponent getActionMap

Introduction

In this page you can find the example usage for javax.swing JComponent getActionMap.

Prototype

public final ActionMap getActionMap() 

Source Link

Document

Returns the ActionMap used to determine what Action to fire for particular KeyStroke binding.

Usage

From source file:Main.java

public static void bindAction(JComponent aComponent, String aCommand, Action anAction, KeyStroke aKeyStroke) {
    aComponent.getInputMap(JComponent.WHEN_FOCUSED).put(aKeyStroke, aCommand);
    aComponent.getActionMap().put(aCommand, anAction);
}

From source file:Main.java

public static void setKeyAction_WhenFocused(JComponent component, int keyCode, String actionIdString,
        Action action) {//w w  w  . j  av a 2  s.  c om
    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(keyCode, 0), actionIdString);
    component.getActionMap().put(actionIdString, 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./*www. j av  a2  s.c o 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

/**
 * //  w  w w . j  a  va  2 s  .  co 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

/**
 * Sets the hot key for focus.//from   w w w . j ava  2 s .c  om
 *
 * @param comp
 *            the comp
 * @param keyStroke
 *            the key stroke
 * @param actionName
 *            the action name
 */
public static void setHotKeyForFocus(final JComponent comp, final String keyStroke, final String actionName) {
    // get the button's Action map
    final ActionMap amap = comp.getActionMap();
    // add an action to the button's action map
    // and give it a name(it can be any object not just String)
    amap.put(actionName, new AbstractAction() {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(final ActionEvent e) {
            // call your a method that contains your action code
            comp.requestFocus();
        }
    });
    // get the input map for the button
    final InputMap imap = comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    // add a key stroke associated with an action in the action map(action
    // name).
    // imap.put(KeyStroke.getKeyStroke("F1"),"ActionName");
    // you can do the same for more than one key.
    imap.put(KeyStroke.getKeyStroke(keyStroke), actionName);
}

From source file:Main.java

public static void setKeyAction_WhenInFocusedWindow(JComponent component, int keyCode, String actionIdString,
        Action action) {//from  ww  w  . j ava2  s. c  o  m
    component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(keyCode, 0),
            actionIdString);
    component.getActionMap().put(actionIdString, action);
}

From source file:Main.java

public static void associaTeclaAtalho(JComponent component, Action action, String nomeAcao, String... atalhos) {
    final InputMap inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    final ActionMap actionMap = component.getActionMap();
    for (String atalho : atalhos) {
        KeyStroke keyStroke = KeyStroke.getKeyStroke(atalho);
        inputMap.put(keyStroke, nomeAcao);
        actionMap.put(nomeAcao, action);
    }//  w w w  .  j av a  2s. c  o  m
}

From source file:Main.java

/**
 * Adds a component action./*from www .  j a va  2  s. 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

/**
 * Registers an action for a key-stroke in a component.
 * @param actionName The name for the action related to the keystroke. If null, the keystroke description is used.
 * @param focusType e.g. {@link JComponent#WHEN_FOCUSED}
 * @param ks The keystroke that activates the action.
 *//*w  ww.  j  av a 2  s  . c o  m*/
public static void setKeyAction(JComponent component, Action action, String actionName, int focusType,
        KeyStroke ks) {

    if (actionName == null) {
        actionName = ks.toString();
    }
    component.getInputMap(focusType).put(ks, actionName);
    component.getActionMap().put(actionName, action);
}

From source file:Main.java

public static void addAction(JComponent component, String keyStroke, AbstractAction action) {
    KeyStroke ks = KeyStroke.getKeyStroke(keyStroke);
    if (ks == null)
        throw new IllegalArgumentException("invalid key stroke: " + keyStroke);
    Object key = ks + "-" + System.currentTimeMillis();
    component.getActionMap().put(key, action);
    component.getInputMap().put(ks, key);

}