Java Swing Key Action simulateEnterKeyPressed(final Component component, final int delayInMilliseconds)

Here you can find the source of simulateEnterKeyPressed(final Component component, final int delayInMilliseconds)

Description

simulate Enter Key Pressed

License

LGPL

Declaration

public static void simulateEnterKeyPressed(final Component component, final int delayInMilliseconds) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.awt.AWTEvent;
import java.awt.Component;

import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;

import javax.swing.SwingUtilities;

public class Main {
    public static void simulateEnterKeyPressed(final Component component, final int delayInMilliseconds) {
        final Runnable pressEnter = new Runnable() {
            @Override/*  w ww  . j  a v a 2 s  . c  o m*/
            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");
                    f.setAccessible(true);
                    f.set(keyEvent, Boolean.TRUE);
                    component.dispatchEvent(keyEvent);
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        };

        invokeDelayed(delayInMilliseconds, pressEnter);
    }

    public static void invokeDelayed(final int delayInMilliseconds, final Runnable action) {
        if (delayInMilliseconds == 0)
            SwingUtilities.invokeLater(action);
        else
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(delayInMilliseconds);
                        SwingUtilities.invokeAndWait(action);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
    }
}

Related

  1. setMnemonic(Action action)
  2. setTabFocusTraversalKeys(final JComponent component)
  3. setupAction(Action action, ResourceBundle bundle, String actionId)
  4. setUseStandardFocusTraversalKeys(Component comp, boolean use)
  5. simulateEnterKey(Component c)
  6. simulateKeyStrokes(Component comp, String text)
  7. stringify(KeyStroke keyStroke)
  8. stringToKey(String s)
  9. stringToKeys(String s)