Java Swing ActionMap addComponentAction(final JComponent component, final Action action)

Here you can find the source of addComponentAction(final JComponent component, final Action action)

Description

Adds a component action.

License

Open Source License

Parameter

Parameter Description
component The compoenet to add the action to
action The action to add

Declaration

public static void addComponentAction(final JComponent component, final Action action) 

Method Source Code

//package com.java2s;
/*//  www .  ja  va2  s . c o m
 * Copyright (C) 2010-2012 Klaus Reimer <k@ailis.de>
 * See LICENSE.TXT for licensing information.
 */

import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;

import javax.swing.KeyStroke;

public class Main {
    /**
     * Adds a component action.
     * 
     * @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);
    }
}

Related

  1. addAction(JComponent component, String kstr, AbstractAction action)
  2. addEnterAction(JComponent c, Action a)
  3. addEnterAction(JComponent comp, Action action)
  4. addFieldsValidateAction(Action action, JComponent... components)
  5. addFieldValidateAction(JComponent field, Action action)