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

voidregisterKey(JComponent component, final int keyEvent, int modifiers, Action action)
register Key
final KeyStroke stroke = KeyStroke.getKeyStroke(keyEvent, modifiers);
component.registerKeyboardAction(action, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
voidregisterKeyAction(JComponent component, int keyCode, Action action)
A simplified version of another method of the same name for registering a handler for certain key events.
registerKeyAction(component, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
        KeyStroke.getKeyStroke(keyCode, 0), action);
voidregisterTabKey(Container container)
Register the tab key with the container.
if (container instanceof JComponent) {
    ((JComponent) container).registerKeyboardAction(new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
    }, KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), JComponent.WHEN_FOCUSED);
} else {
    for (int i = 0; i < container.getComponentCount(); i++) {
...
voidregisterWindowCloseKeys(JRootPane root, Action closeAction)
Registers key events for a Ctrl-W and ESC with an ActionListener that will take care of disposing the window.
voidremoveTabbedPaneFocusTraversalKeyBindings(JComponent c)
Remove problematic actions that prevent Ctrl+PageUp/PageDown from being used for cycling through active documents.
InputMap im = c.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, InputEvent.CTRL_MASK), "nothing");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, InputEvent.CTRL_MASK), "nothing");
im = c.getInputMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, InputEvent.CTRL_MASK), "nothing");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, InputEvent.CTRL_MASK), "nothing");
voidreplaceAction(InputMap map, char c)
Moves the action for the specified character from the 'ctrl' mask to the 'meta' mask.
KeyStroke ctrl = KeyStroke.getKeyStroke("control pressed " + c);
KeyStroke meta = KeyStroke.getKeyStroke("meta pressed " + c);
if (ctrl == null || meta == null)
    return;
Object action = map.get(ctrl);
if (action != null) {
    map.remove(ctrl);
    map.put(meta, action);
...
voidsendKeys(JComponent component, int modifiers, int key)
Send a keys with robot
KeyEvent event = new KeyEvent(component, KeyEvent.KEY_PRESSED, 0, modifiers, key, KeyEvent.CHAR_UNDEFINED,
        KeyEvent.KEY_LOCATION_STANDARD);
component.requestFocusInWindow();
component.dispatchEvent(event);
voidsetActionID(final Action a, final String id)
Make sure that #getActionID(Action) returns the passed string.
a.putValue(Action.ACTION_COMMAND_KEY, id);
voidsetDefaultOkCancelKeyStrokes(final JRootPane rootPane, final Action okAction, final Action cancelAction)
set Default Ok Cancel Key Strokes
final InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
if (okAction != null) {
    final String OK_ACTION_KEY = "OK_ACTION_KEY";
    final KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
    inputMap.put(enterKey, OK_ACTION_KEY);
    rootPane.getActionMap().put(OK_ACTION_KEY, okAction);
if (cancelAction != null) {
...
voidsetEscapeKeyboardAction(final Window window, JComponent pane)
Registers the ESCAPE key on the Panel so that it closes the Dialog.
final KeyEventDispatcher dispatcher = new KeyEventDispatcher() {
    @Override
    public boolean dispatchKeyEvent(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE && e.getID() == KeyEvent.KEY_PRESSED
                && window.isFocused()) {
            window.dispose();
            return true;
        return false;
};
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
window.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);
    @Override
    public void windowClosed(WindowEvent e) {
        KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);
});