src/share/classes/javax/swing/SwingUtilities.java

Print this page




1263      * on the event dispatching thread and
1264      * then prints a message.
1265      * <pre>
1266      * Runnable doHelloWorld = new Runnable() {
1267      *     public void run() {
1268      *         System.out.println("Hello World on " + Thread.currentThread());
1269      *     }
1270      * };
1271      *
1272      * SwingUtilities.invokeLater(doHelloWorld);
1273      * System.out.println("This might well be displayed before the other message.");
1274      * </pre>
1275      * If invokeLater is called from the event dispatching thread --
1276      * for example, from a JButton's ActionListener -- the <i>doRun.run()</i> will
1277      * still be deferred until all pending events have been processed.
1278      * Note that if the <i>doRun.run()</i> throws an uncaught exception
1279      * the event dispatching thread will unwind (not the current thread).
1280      * <p>
1281      * Additional documentation and examples for this method can be
1282      * found in
1283      * <A HREF="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency in Swing</a>.
1284      * <p>
1285      * As of 1.3 this method is just a cover for <code>java.awt.EventQueue.invokeLater()</code>.
1286      * <p>
1287      * Unlike the rest of Swing, this method can be invoked from any thread.
1288      *
1289      * @see #invokeAndWait
1290      */
1291     public static void invokeLater(Runnable doRun) {
1292         EventQueue.invokeLater(doRun);
1293     }
1294 
1295 
1296     /**
1297      * Causes <code>doRun.run()</code> to be executed synchronously on the
1298      * AWT event dispatching thread.  This call blocks until
1299      * all pending AWT events have been processed and (then)
1300      * <code>doRun.run()</code> returns. This method should
1301      * be used when an application thread needs to update the GUI.
1302      * It shouldn't be called from the event dispatching thread.
1303      * Here's an example that creates a new application thread


1314      * Thread appThread = new Thread() {
1315      *     public void run() {
1316      *         try {
1317      *             SwingUtilities.invokeAndWait(doHelloWorld);
1318      *         }
1319      *         catch (Exception e) {
1320      *             e.printStackTrace();
1321      *         }
1322      *         System.out.println("Finished on " + Thread.currentThread());
1323      *     }
1324      * };
1325      * appThread.start();
1326      * </pre>
1327      * Note that if the <code>Runnable.run</code> method throws an
1328      * uncaught exception
1329      * (on the event dispatching thread) it's caught and rethrown, as
1330      * an <code>InvocationTargetException</code>, on the caller's thread.
1331      * <p>
1332      * Additional documentation and examples for this method can be
1333      * found in
1334      * <A HREF="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency in Swing</a>.
1335      * <p>
1336      * As of 1.3 this method is just a cover for
1337      * <code>java.awt.EventQueue.invokeAndWait()</code>.
1338      *
1339      * @exception  InterruptedException if we're interrupted while waiting for
1340      *             the event dispatching thread to finish excecuting
1341      *             <code>doRun.run()</code>
1342      * @exception  InvocationTargetException  if an exception is thrown
1343      *             while running <code>doRun</code>
1344      *
1345      * @see #invokeLater
1346      */
1347     public static void invokeAndWait(final Runnable doRun)
1348         throws InterruptedException, InvocationTargetException
1349     {
1350         EventQueue.invokeAndWait(doRun);
1351     }
1352 
1353     /**
1354      * Returns true if the current thread is an AWT event dispatching thread.




1263      * on the event dispatching thread and
1264      * then prints a message.
1265      * <pre>
1266      * Runnable doHelloWorld = new Runnable() {
1267      *     public void run() {
1268      *         System.out.println("Hello World on " + Thread.currentThread());
1269      *     }
1270      * };
1271      *
1272      * SwingUtilities.invokeLater(doHelloWorld);
1273      * System.out.println("This might well be displayed before the other message.");
1274      * </pre>
1275      * If invokeLater is called from the event dispatching thread --
1276      * for example, from a JButton's ActionListener -- the <i>doRun.run()</i> will
1277      * still be deferred until all pending events have been processed.
1278      * Note that if the <i>doRun.run()</i> throws an uncaught exception
1279      * the event dispatching thread will unwind (not the current thread).
1280      * <p>
1281      * Additional documentation and examples for this method can be
1282      * found in
1283      * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency in Swing</a>.
1284      * <p>
1285      * As of 1.3 this method is just a cover for <code>java.awt.EventQueue.invokeLater()</code>.
1286      * <p>
1287      * Unlike the rest of Swing, this method can be invoked from any thread.
1288      *
1289      * @see #invokeAndWait
1290      */
1291     public static void invokeLater(Runnable doRun) {
1292         EventQueue.invokeLater(doRun);
1293     }
1294 
1295 
1296     /**
1297      * Causes <code>doRun.run()</code> to be executed synchronously on the
1298      * AWT event dispatching thread.  This call blocks until
1299      * all pending AWT events have been processed and (then)
1300      * <code>doRun.run()</code> returns. This method should
1301      * be used when an application thread needs to update the GUI.
1302      * It shouldn't be called from the event dispatching thread.
1303      * Here's an example that creates a new application thread


1314      * Thread appThread = new Thread() {
1315      *     public void run() {
1316      *         try {
1317      *             SwingUtilities.invokeAndWait(doHelloWorld);
1318      *         }
1319      *         catch (Exception e) {
1320      *             e.printStackTrace();
1321      *         }
1322      *         System.out.println("Finished on " + Thread.currentThread());
1323      *     }
1324      * };
1325      * appThread.start();
1326      * </pre>
1327      * Note that if the <code>Runnable.run</code> method throws an
1328      * uncaught exception
1329      * (on the event dispatching thread) it's caught and rethrown, as
1330      * an <code>InvocationTargetException</code>, on the caller's thread.
1331      * <p>
1332      * Additional documentation and examples for this method can be
1333      * found in
1334      * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency in Swing</a>.
1335      * <p>
1336      * As of 1.3 this method is just a cover for
1337      * <code>java.awt.EventQueue.invokeAndWait()</code>.
1338      *
1339      * @exception  InterruptedException if we're interrupted while waiting for
1340      *             the event dispatching thread to finish excecuting
1341      *             <code>doRun.run()</code>
1342      * @exception  InvocationTargetException  if an exception is thrown
1343      *             while running <code>doRun</code>
1344      *
1345      * @see #invokeLater
1346      */
1347     public static void invokeAndWait(final Runnable doRun)
1348         throws InterruptedException, InvocationTargetException
1349     {
1350         EventQueue.invokeAndWait(doRun);
1351     }
1352 
1353     /**
1354      * Returns true if the current thread is an AWT event dispatching thread.