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

voiddumpActionMap(JComponent comp)
dump Action Map
System.out.println("InputMap for WHEN_ANCESTOR_OF_FOCUSED_COMPONENT");
System.out.println("-----------------------------------------------");
dumpInputMap(comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT));
System.out.println("\nInputMap for WHEN_FOCUSED");
System.out.println("-------------------------");
dumpInputMap(comp.getInputMap(JComponent.WHEN_FOCUSED));
System.out.println("\nInputMap for WHEN_IN_FOCUSED_WINDOW");
System.out.println("-----------------------------------");
...
InputMapfillInputMap(ActionMap am)
fill Input Map
InputMap keysMap = new InputMap();
if (am != null) {
    Object[] oNames = am.allKeys();
    if (oNames != null) {
        for (int i = 0; i < oNames.length; i++) {
            Action action = am.get(oNames[i]);
            Object oKey = action.getValue(Action.ACCELERATOR_KEY);
            if (oKey != null && oKey instanceof KeyStroke) {
...
voidinstallActions(JComponent comp, Action actions[], int condition)
install Actions
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);
voidprintActionInputMap(JComponent comp)
Print Action and Input Map for component
ActionMap am = comp.getActionMap();
Object[] amKeys = am.allKeys(); 
if (amKeys != null) {
    System.out.println("-------------------------");
    System.out.println("ActionMap for Component " + comp.toString());
    for (int i = 0; i < amKeys.length; i++) {
        Action a = am.get(amKeys[i]);
        StringBuffer sb = new StringBuffer("- ");
...
voidprintActionMap(ActionMap actionMap, String who)
print Action Map
System.out.println("Action map for " + who + ":");
Object[] keys = actionMap.allKeys();
if (keys != null) {
    for (int i = 0; i < keys.length; i++) {
        Object key = keys[i];
        Action targetAction = actionMap.get(key);
        System.out.println("\tName: <" + key + ">, action: " + targetAction.getClass().getName());
voidputAction(Action action)
put Action
actionMap.put(action.getValue(Action.NAME), action);
voidputAction(JComponent component, Action action)
put Action
KeyStroke keyStroke = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
String name = (String) action.getValue(Action.NAME);
component.getInputMap().put(keyStroke, name);
component.getActionMap().put(name, action);
voidregisterAction(JComponent component, int condition, Action action, String command)
register Action
component.getInputMap(condition).put((KeyStroke) action.getValue(AbstractAction.ACCELERATOR_KEY), command);
component.getActionMap().put(command, action);
voidregisterUndoRedoActions(JComponent jtc, Action undoAction, Action redoAction)
register Undo Redo Actions
InputMap inputMap = jtc.getInputMap();
inputMap.put(KeyStroke.getKeyStroke("pressed UNDO"), "undo");
inputMap.put(KeyStroke.getKeyStroke("ctrl pressed Z"), "undo");
inputMap.put(KeyStroke.getKeyStroke("meta pressed Z"), "undo");
inputMap.put(KeyStroke.getKeyStroke("pressed REDO"), "redo");
inputMap.put(KeyStroke.getKeyStroke("ctrl pressed Y"), "redo");
inputMap.put(KeyStroke.getKeyStroke("meta pressed Y"), "redo");
ActionMap actionMap = jtc.getActionMap();
...
voidremoveAction(InputMap im, ActionMap am, String name)
remove Action
Action a = am.get(name);
if (a != null) {
    KeyStroke ks = (KeyStroke) a.getValue(Action.ACCELERATOR_KEY);
    if (ks != null) {
        im.remove(ks);
    am.remove(name);