Java Utililty Methods Swing Key Action

List of utility methods to do Swing Key Action

Description

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

Method

voidaddActionsToMap(Action[] list1, Map h)
add a col;lections of actions to a Map where the key is the action name
for (int i = 0; i < list1.length; i++) {
    Action a = list1[i];
    String value = (String) a.getValue(Action.NAME);
    h.put((value != null ? value : ""), a);
voidaddCloseActionWithEscapeKey(final Window window, JRootPane rootPane)
Adds the close action with escape key.
KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
Action escapeAction = new AbstractAction() {
    private static final long serialVersionUID = 0L;
    @Override
    public void actionPerformed(ActionEvent e) {
        window.setVisible(false);
};
...
voidaddCtrlEnterAction(JComponent comp, AbstractAction action)
add Ctrl Enter Action
InputMap inputMap = comp.getInputMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK), action);
voidaddEnterKey(JComponent control)
The source for this is http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html
Set forwardKeys = control.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
Set newForwardKeys = new HashSet(forwardKeys);
newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
control.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newForwardKeys);
voidaddEscKeyAction(javax.swing.JComponent component, javax.swing.InputMap inputMap, javax.swing.Action action)
add Esc Key Action
inputMap.put(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0, false), "ESC_ACTION");
component.getActionMap().put("ESC_ACTION", action);
voidaddHotKey(int key, JComponent to, String actionName, Action action)
Adds a control hot key to the containing window of a component.
KeyStroke keystroke = KeyStroke.getKeyStroke(key, java.awt.event.InputEvent.CTRL_MASK);
InputMap map = to.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
map.put(keystroke, actionName);
to.getActionMap().put(actionName, action);
if (to instanceof JMenuItem) {
    ((JMenuItem) to).setAccelerator(keystroke);
if (to instanceof AbstractButton) 
...
voidaddIcons(Action action, String[][] iconRoles)
Add icons to an action.
for (String[] iconRole : iconRoles) {
    URL img = action.getClass().getResource(iconRole[0]);
    if (img != null) {
        ImageIcon icon = new ImageIcon(img);
        action.putValue(iconRole[1], icon);
voidaddStrokeToName(Action action)
add Stroke To Name
KeyStroke stroke = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
if (stroke == null)
    return;
String name = String.format("%s%s - %s", KeyEvent.getKeyModifiersText(stroke.getModifiers()),
        KeyEvent.getKeyText(stroke.getKeyCode()), action.getValue(Action.NAME));
action.putValue(Action.NAME, name);
Action[]augmentList(Action[] list1, Action[] list2)
Takes one list of commands and augments it with another list of commands.
Map<String, Action> h = new HashMap<String, Action>();
addActionsToMap(list1, h);
addActionsToMap(list2, h);
Action[] actions = mapToActionList(h);
return actions;
voiddispatchEvent(final KeyEvent ke, final Component comp)
dispatch Event
if (!SwingUtilities.isEventDispatchThread()) {
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            comp.dispatchEvent(ke);
    });
} else {
...