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

voidescapeKeyAction(JComponent component, javax.swing.AbstractAction abstractAction)
escape Key Action
component.getRootPane().getActionMap().put(ESC_ACTION_KEY, abstractAction);
component.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
        .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), ESC_ACTION_KEY);
StringformatKeyStroke(final KeyStroke keyStroke)
pretty print a keystroke.
final String keyModifiersText = KeyEvent.getKeyModifiersText(keyStroke.getModifiers());
final String keyText = KeyEvent.getKeyText(keyStroke.getKeyCode());
return keyModifiersText.length() == 0 ? keyText : keyModifiersText + "+" + keyText;
StringgetActionID(final Action a)
Tries to find an ID for the passed action from its Action#getValue(String) properties .
Object actionID = a.getValue(Action.ACTION_COMMAND_KEY);
if (actionID == null)
    actionID = a.getValue(Action.NAME);
return actionID == null ? null : actionID.toString();
StringgetActionInstanceName(Action delegate)
get Action Instance Name
Object commandKey = delegate.getValue(Action.ACTION_COMMAND_KEY);
String id;
if (commandKey != null && !commandKey.toString().isEmpty()) {
    id = commandKey.toString();
} else {
    id = delegate.getClass().getName();
id = id.replace('/', '-').replace('.', '-').replace('$', '-');
...
KeyStrokegetEscapeKeystroke()
get Escape Keystroke
return KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
KeyStrokegetKeyStroke(int i0, int i1)
get Key Stroke
if ((i1 & KeyEvent.META_MASK) != 0) {
    if (!onMac) {
        i1 = i1 - KeyEvent.META_MASK + KeyEvent.CTRL_MASK;
KeyStroke ks = KeyStroke.getKeyStroke(i0, i1);
if (debug) {
    Exception ex = new Exception();
...
KeyStrokegetKeystroke(int keyevent)
Returns a cross-platform keystroke that enables the platform behave natively.
return KeyStroke.getKeyStroke(keyevent, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
KeyStrokegetKeyStrokeCopy()
get Key Stroke Copy
return KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK, false);
StringgetKeyStrokeRepresentation(KeyStroke ks)
Returns a String representation for the given KeyStroke for display, in the following format:
modifier+modifier+...+key

For example, KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK|InputEvent.ALT_MASK) will return Ctrl+Alt+C.

return ks.toString().replaceFirst("(released )|(pressed )|(typed )", "");
StringgetKeyStrokeText(KeyStroke ks)
get Key Stroke Text
StringBuilder sb = new StringBuilder();
String modifierText = KeyEvent.getKeyModifiersText(ks.getModifiers());
sb.append(modifierText);
String keyText = KeyEvent.getKeyText(ks.getKeyCode());
if (!keyText.isEmpty() && !modifierText.contains(keyText)) {
    if (sb.length() > 0) {
        sb.append('+');
    sb.append(keyText);
return sb.toString();