Java Utililty Methods Swing ActionMap

List of utility methods to do Swing ActionMap

Description

The list of methods to do Swing ActionMap are organized into topic(s).

Method

voidaddAction(JComponent component, String kstr, AbstractAction action)
add Action
KeyStroke ks = KeyStroke.getKeyStroke(kstr);
component.getInputMap().put(ks, ks);
component.getActionMap().put(ks, action);
voidaddComponentAction(final JComponent component, final Action action)
Adds a component 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);
voidaddEnterAction(JComponent c, Action a)
add Enter Action
c.getActionMap().put("enter", a);
c.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
voidaddEnterAction(JComponent comp, Action action)
add Enter Action
comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), action);
comp.getActionMap().put(action, action);
voidaddFieldsValidateAction(Action action, JComponent... components)
Add an action to execute when the validation key (Enter) is pressed on all the given components.
for (JComponent component : components) {
    addFieldValidateAction(component, action);
voidaddFieldValidateAction(JComponent field, Action action)
Add an action to execute when the validation key (Enter) is pressed.
field.getActionMap().put("validate", action);
field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "validate");
voidattachAccelerator(Action action, JComponent component)
attach Accelerator
KeyStroke stroke = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
String name = (String) action.getValue(Action.NAME);
component.getInputMap(JComponent.WHEN_FOCUSED).put(stroke, name);
component.getActionMap().put(name, action);
addStrokeToName(action);
voidbindAction(JComponent aComponent, String aCommand, Action anAction)
bind Action
Object o = anAction.getValue(Action.ACCELERATOR_KEY);
if (o instanceof KeyStroke)
    bindAction(aComponent, aCommand, anAction, (KeyStroke) o);
else
    throw new IllegalArgumentException("Can not bind action, it has no keystroke assigned.");
voidcheckActions(JComponent aComponent)
check Actions
ActionMap am = aComponent.getActionMap();
for (Object key : am.allKeys()) {
    Action action = am.get(key);
    if (action != null) {
        action.setEnabled(action.isEnabled());
ActionMapcloneActionMap(ActionMap original)
clone Action Map
ActionMap map = new ActionMap();
Object[] keys = original.keys();
if (keys != null) {
    for (Object key : keys) {
        map.put(key, original.get(key));
return map;
...