Example usage for javax.swing JComponent getInputMap

List of usage examples for javax.swing JComponent getInputMap

Introduction

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

Prototype

public final InputMap getInputMap(int condition) 

Source Link

Document

Returns the InputMap that is used during condition.

Usage

From source file:Main.java

/**
 * Registers the keystroke of the given action as "command" of the given
 * component.//ww  w .j  av  a2  s.co  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:de.ailis.xadrian.utils.SwingUtils.java

/**
 * Adds a component action.//from   w  ww  .j a  va 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.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//  w  w  w  .j a  v  a 2  s. co  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:Main.java

/**
 * Sets the hot key for focus./* w w  w  .j  a v a  2s .  co m*/
 *
 * @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: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  .  j a v  a  2s  .  c  om*/
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:com.limegroup.gnutella.gui.GUIUtils.java

/**
 * Fixes the InputMap to have the correct KeyStrokes registered for
 * actions on various OS's.//  ww w. j  a  va2s . c  o m
 *
 * Currently, this fixes OSX to use the 'meta' key instead of hard-coding
 * it to use the 'control' key for actions such as 'select all', etc..
 */
public static void fixInputMap(JComponent jc) {
    InputMap map = jc.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    if (OSUtils.isMacOSX()) {
        replaceAction(map, 'A'); // select all
        replaceAction(map, 'C'); // copy
        replaceAction(map, 'V'); // paste
        replaceAction(map, 'X'); // cut
    }
}

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  w  w. ja va 2 s.com
 *  
 * @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: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  va2s  . c  o m*/
        public void actionPerformed(ActionEvent e) {
            Object selectedItem = comboBox.getSelectedItem();
            if (selectedItem != null) {
                model.addElement((String) selectedItem);
            }
        }
    });
}

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:net.pandoragames.far.ui.swing.dialog.SubWindow.java

/**
 * Registers Ctrl + w as a window close event on the specified component;
 * /*from w w w .  ja  v a  2 s . co 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);
}