Here you can find the source of dispatchEvent(final KeyEvent ke, final Component comp)
private static void dispatchEvent(final KeyEvent ke, final Component comp) throws Exception
//package com.java2s; import java.awt.Component; import java.awt.event.KeyEvent; import javax.swing.SwingUtilities; public class Main { private static void dispatchEvent(final KeyEvent ke, final Component comp) throws Exception { if (!SwingUtilities.isEventDispatchThread()) { SwingUtilities.invokeAndWait(new Runnable() { @Override//from w w w. j av a 2s.co m public void run() { comp.dispatchEvent(ke); } }); } else { comp.dispatchEvent(ke); } } }