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

voidsetKeyEvent(JComponent widget, int keyEvent, Runnable action)
set Key Event
String inputMapKey = "key_" + keyEvent;
widget.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(keyEvent, 0), inputMapKey);
widget.getActionMap().put(inputMapKey, new AbstractAction() {
    @Override
    public void actionPerformed(final ActionEvent e) {
        action.run();
});
...
voidsetMnemonic(Action action)
Translate the character following MNEMONIC_CHARACTER in the label by a mnemonic
String actionLabel = (String) action.getValue(Action.NAME);
if (actionLabel != null) {
    int charPosition = getMnemonicCharPos(actionLabel);
    if (charPosition >= 0) {
        action.putValue(Action.MNEMONIC_KEY,
                new Integer(Character.toUpperCase(actionLabel.charAt(charPosition + 1))));
        action.putValue(Action.NAME, clearMnemonic(charPosition, actionLabel));
voidsetTabFocusTraversalKeys(final JComponent component)
set Tab Focus Traversal Keys
component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
        new HashSet<AWTKeyStroke>(Arrays.asList(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0))));
component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, new HashSet<AWTKeyStroke>(
        Arrays.asList(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_DOWN_MASK))));
voidsetupAction(Action action, ResourceBundle bundle, String actionId)
Sets action values according to values specified by resource bundle.
action.putValue(Action.NAME, bundle.getString(actionId + ACTION_NAME_POSTFIX));
action.putValue(ACTION_ID, actionId);
if (bundle.containsKey(actionId + ACTION_SHORT_DESCRIPTION_POSTFIX)) {
    action.putValue(Action.SHORT_DESCRIPTION,
            bundle.getString(actionId + ACTION_SHORT_DESCRIPTION_POSTFIX));
if (bundle.containsKey(actionId + ACTION_SMALL_ICON_POSTFIX)) {
    action.putValue(Action.SMALL_ICON, new javax.swing.ImageIcon(
...
voidsetUseStandardFocusTraversalKeys(Component comp, boolean use)
Sets whether the given Component uses tab and shift-tab as forward/backward focus traversal keys.
modifyFocusTraversalKeys(comp, true, use, KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
modifyFocusTraversalKeys(comp, false, use,
        KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK));
voidsimulateEnterKey(Component c)
Example came from http://tech.chitgoks.com/2010/08/31/simulate-enter-key-on -any-component-using-java/
try {
    KeyEvent ke = new KeyEvent(c, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), -1, KeyEvent.VK_ENTER,
            (char) KeyEvent.VK_ENTER);
    c.requestFocusInWindow();
    dispatchEvent(ke, c);
} catch (Exception ex) {
voidsimulateEnterKeyPressed(final Component component, final int delayInMilliseconds)
simulate Enter Key Pressed
final Runnable pressEnter = new Runnable() {
    @Override
    public void run() {
        KeyEvent keyEvent = new KeyEvent(component, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0,
                KeyEvent.VK_ENTER, (char) KeyEvent.VK_ENTER, 1);
        java.lang.reflect.Field f;
        try {
            f = AWTEvent.class.getDeclaredField("focusManagerIsDispatching");
...
voidsimulateKeyStrokes(Component comp, String text)
simulate Key Strokes
for (int i = 0; i < text.length(); i++) {
    char c = text.charAt(i);
    simulateKeyStroke(comp, c);
Stringstringify(KeyStroke keyStroke)
stringify
if (keyStroke == null) {
    return null;
int kc = keyStroke.getKeyCode();
int modifiers = keyStroke.getModifiers();
StringBuilder buf = new StringBuilder();
if ((modifiers & InputEvent.META_DOWN_MASK) != 0) {
    buf.append("cmd+");
...
KeyStrokestringToKey(String s)
Construct a new key description from a given universal string description.
StringTokenizer st = new StringTokenizer(s.toUpperCase(), "-", true); 
int needed = 0;
HashMap names = initNameAndValues()[0];
int lastModif = -1;
try {
    for (;;) {
        String el = st.nextToken();
        if (el.equals("-")) { 
...