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

Print this page




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


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




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


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