Example usage for javax.swing ActionMap put

List of usage examples for javax.swing ActionMap put

Introduction

In this page you can find the example usage for javax.swing ActionMap put.

Prototype

public void put(Object key, Action action) 

Source Link

Document

Adds a binding for key to action.

Usage

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);
    }//w ww. j a  v a  2 s  .  c  o  m
}

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);
    }//from   w ww. j ava  2  s.  co m
}

From source file:com.projity.pm.graphic.spreadsheet.common.transfer.NodeListTransferHandler.java

public static void registerWith(SpreadSheet spreadSheet) {
    NodeListTransferHandler handler = new NodeListTransferHandler(spreadSheet);
    //          if (c instanceof SpreadSheet){
    //             SpreadSheet spreadSheet=(SpreadSheet)c;
    //             handler.setSpreadSheet(spreadSheet);
    //          }
    spreadSheet.setTransferHandler(handler);

    InputMap imap = spreadSheet.getInputMap();
    imap.put(KeyStroke.getKeyStroke("ctrl X"), NodeListTransferHandler.getCutAction().getValue(Action.NAME));
    imap.put(KeyStroke.getKeyStroke("ctrl C"), NodeListTransferHandler.getCopyAction().getValue(Action.NAME));
    imap.put(KeyStroke.getKeyStroke("ctrl V"), NodeListTransferHandler.getPasteAction().getValue(Action.NAME));
    //c.setInputMap(JComponent.WHEN_FOCUSED,imap);

    ActionMap amap = spreadSheet.getActionMap();
    amap.put(NodeListTransferHandler.getCutAction().getValue(Action.NAME),
            NodeListTransferHandler.getCutAction());
    amap.put(NodeListTransferHandler.getCopyAction().getValue(Action.NAME),
            NodeListTransferHandler.getCopyAction());
    amap.put(NodeListTransferHandler.getPasteAction().getValue(Action.NAME),
            NodeListTransferHandler.getPasteAction());

}

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

/**
 * Adds a component action.//  w  w  w  .ja  va 2s. 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 ava  2s  .  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:net.sf.jabref.util.Util.java

/**
 * Binds ESC-Key to cancel button/* ww w  .jav a2 s.  c o m*/
 *
 * @param rootPane     the pane to bind the action to. Typically, this variable is retrieved by this.getRootPane();
 * @param cancelAction the action to bind
 */
// TODO: move to GUI
public static void bindCloseDialogKeyToCancelAction(JRootPane rootPane, Action cancelAction) {
    InputMap im = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = rootPane.getActionMap();
    im.put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE_DIALOG), "close");
    am.put("close", cancelAction);
}

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.//from  w  ww . j  av  a2 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 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:ActionTest.java

public ActionFrame() {
    setTitle("ActionTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    buttonPanel = new JPanel();

    // define actions
    Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"), Color.YELLOW);
    Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.BLUE);
    Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.RED);

    // add buttons for these actions
    buttonPanel.add(new JButton(yellowAction));
    buttonPanel.add(new JButton(blueAction));
    buttonPanel.add(new JButton(redAction));

    // add panel to frame
    add(buttonPanel);/*from   ww w  .  ja v  a 2  s .  c  o m*/

    // associate the Y, B, and R keys with names
    InputMap imap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    imap.put(KeyStroke.getKeyStroke("ctrl Y"), "panel.yellow");
    imap.put(KeyStroke.getKeyStroke("ctrl B"), "panel.blue");
    imap.put(KeyStroke.getKeyStroke("ctrl R"), "panel.red");

    // associate the names with actions
    ActionMap amap = buttonPanel.getActionMap();
    amap.put("panel.yellow", yellowAction);
    amap.put("panel.blue", blueAction);
    amap.put("panel.red", redAction);
}

From source file:edu.ku.brc.af.ui.forms.ResultSetController.java

/**
 * Sets the current (or focused ResultSetController) into the action btns.
 * @param rsc ResultSetController//from ww  w  .j  a v  a 2s  .  c  om
 */
protected static void installRS(final ResultSetController rsc) {
    if (commandsHash == null && rsc != null) {
        rsc.createRSActions();
    }

    if (commandsHash != null) {
        for (RSAction<CommandType> rsca : commandsHash.values()) {
            rsca.setRs(rsc);

            if (rsc != null) {
                JButton btn = rsc.btnsHash.get(rsca.getType());
                if (btn != null) {
                    KeyStroke ks = UIHelper.getKeyStroke(rsca.getType());
                    String ACTION_KEY = rsca.getType().toString();
                    InputMap inputMap = btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
                    ActionMap actionMap = btn.getActionMap();

                    inputMap.put(ks, ACTION_KEY);
                    actionMap.put(ACTION_KEY, rsca);
                    rsca.setBtn(btn);
                } else {
                    //System.err.println("Btn for ["+rsca.getType()+"] is null");
                }
            }
        }
    }
}