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

/**
 * Obtine actiunea inregistrata pentru apasarea de tasta
 * @return /*from  w  w w .j ava 2s . co  m*/
 */
public static Action getActionForKeystroke(JComponent component, int inputMapId, KeyStroke keyStroke) {
    //Identify the action key
    InputMap inputMap = component.getInputMap(inputMapId);
    String key = (String) inputMap.get(keyStroke);

    //Get the action
    if (key != null) {
        ActionMap localMap = component.getActionMap();
        return localMap.get(key);
    } else {
        return null;
    }
}

From source file:Main.java

public static void refleshAction(JComponent com, KeyStroke keyStroke) {
    InputMap im = com.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    Object o = im.get(keyStroke);
    if (o == null) {
        im = com.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        o = im.get(keyStroke);/*  w  w  w. j  av  a2 s.  c  o  m*/
    }
    if (o != null) {
        Action a = com.getActionMap().get(o);
        a.setEnabled(a.isEnabled());
    }
}

From source file:Main.java

/**
 * Registers the keystroke of the given action as "command" of the given
 * component.//  w w  w. j a  v  a2 s .  c  o m
 * <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.haulmont.cuba.desktop.gui.components.DesktopComponentsHelper.java

/**
 * Add shortcut action to any JComponent.
 *
 * @param name name of action that used as action key in {@link InputMap} and {@link ActionMap}.
 * @param component/*from w ww .  j  a v  a2s.  c o m*/
 * @param key
 * @param action
 */
public static void addShortcutAction(String name, JComponent component, KeyStroke key, Action action) {
    ActionMap actionMap = component.getActionMap();
    InputMap inputMap = component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(key, name);
    actionMap.put(name, action);
}

From source file:de.ailis.xadrian.utils.SwingUtils.java

/**
 * Adds a component action./*from   w  w  w.  j  av  a 2  s.  co m*/
 * 
 * @param component
 *            The compoenet 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:com.limegroup.gnutella.gui.GUIUtils.java

/**
 * Binds a key stroke to the given action for the component. The action is 
 * triggered when the key is pressed and the keyboard focus is withing the
 * specifiedd scope.//  w  ww .j  a  v  a  2 s . co m
 *  
 * @param c component for which the keybinding is installed
 * @param key the key that triggers the action
 * @param a the action
 * @param focusScope one of {@link JComponent.WHEN_FOCUSED},
 * {@link JComponent.WHEN_IN_FOCUSED_WINDOW},
 * {@link JComponent.WHEN_ANCESTOR_OF_FOCUSED_WINDOW}
 */
public static void bindKeyToAction(JComponent c, KeyStroke key, Action a, int focusScope) {
    InputMap inputMap = c.getInputMap(focusScope);
    ActionMap actionMap = c.getActionMap();
    if (inputMap != null && actionMap != null) {
        inputMap.put(key, a);
        actionMap.put(a, a);
    }
}

From source file:com.limegroup.gnutella.gui.GUIUtils.java

/**
 * Adds an action to hide a window / dialog.
 *
 * On OSX, this is done by typing 'Command-W'.
 * On all other platforms, this is done by hitting 'ESC'.
 *///ww w  .ja  va2s  . c o m
public static void addHideAction(JComponent jc) {
    InputMap map = jc.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    map.put(getHideKeystroke(), "limewire.hideWindow");
    jc.getActionMap().put("limewire.hideWindow", getDisposeAction());
}

From source file:Main.java

private void addKeyBind(JComponent contentPane, String key) {
    InputMap iMap = contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap aMap = contentPane.getActionMap();
    iMap.put(KeyStroke.getKeyStroke(key), DISABLE_CLICKER);
    aMap.put(DISABLE_CLICKER, disableButtonAction);
}

From source file:Main.java

private void addKeyBindings(JComponent c) {
    c.getInputMap(JComboBox.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ENTER"),
            "doSomething");
    c.getActionMap().put("doSomething", new AbstractAction() {

        @Override/*from   w  w w .  j a  va  2 s.  c o m*/
        public void actionPerformed(ActionEvent e) {
            Object selectedItem = comboBox.getSelectedItem();
            if (selectedItem != null) {
                model.addElement((String) selectedItem);
            }
        }
    });
}

From source file:net.pandoragames.far.ui.swing.dialog.SubWindow.java

/**
 * Registers Ctrl + w as a window close event on the specified component;
 * /*from  www .j  ava 2  s  .  c o  m*/
 * @param component to becomce receptive for ctrl + w
 */
protected void registerCloseWindowKeyListener(JComponent component) {
    component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_DOWN_MASK), "windowCloseAction");
    component.getActionMap().put("windowCloseAction", windowCloseAction);
}