com.denova.ui
Class SwingRunner

java.lang.Object
  |
  +--com.denova.ui.SwingRunner

public class SwingRunner
extends java.lang.Object

Runs SwingUtiltities.invokeAndWait() safely amd more simply. SwingUtiltities.invokeLater() and SwingUtiltities.invokeAndWait() throw an exception if they are called from the Event Dispatch Thread. The versions in this class check SwingUtilities.isEventDispatchThread() to decide whether to invoke the Runnable now or on the EDT. SwingRunner.invokeAndWait() also avoids the overhead of try/catch. SwingRunner also lets you keep your Swing threading code more or less inline, which makes the code clearer. Example:

        SwingRunner.invokeAndWait(new Runnable()
        {
            public void run()
            {
                okButton.setEnabled(enable);
            }
        });
Anonymous classes in Java like the one used above have a couple of restrictions. Their methods can't access variables that are parameters to, or declared as local in, enclosing methods. And they can only have a default constructor with no parameters, so you can't pass in any local variables. So if you are using local variables you need a helper class:
        // "okEnabled" is a parameter to, or declared in, the enclosing class
        
        class OkEnabler implements Runnable
        {
            boolean buttonEnabled;
            
            public OkEnabler(boolean enabled)
            {
                this.enabled = enabled;
            }
            
            public void run()
            {
                okButton.setEnabled(enabled);
            }
        }
        SwingRunner.invokeAndWait(new OkEnabler(okEnabled));
You can use the simpler form with nonlocal variables and finals. Swing components can almost always be declared final. Last modified: 2008-06-03


Field Summary
static java.lang.String SwingRunnerLogName
           
 
Constructor Summary
SwingRunner()
           
 
Method Summary
static void invokeAndWait(java.lang.Runnable doRun)
          Runs doRun.run() and waits for it to finish.
static void invokeLater(java.lang.Runnable doRun)
          This is a convenience method that just invokes SwingUtilities.invokeLater().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SwingRunnerLogName

public static java.lang.String SwingRunnerLogName
Constructor Detail

SwingRunner

public SwingRunner()
Method Detail

invokeAndWait

public static void invokeAndWait(java.lang.Runnable doRun)
Runs doRun.run() and waits for it to finish. If called in the EDT, runs doRun.run() now, else calls SwingUtilities.invokeAndWait(). SwingRunner can change the order of execution. If we're already in the EDT SwingRunner will invoke the Runnable immediately, possibly ahead of Runnables waiting on the EDT. Because this version of invokeAndWait() explicitly checks for the EDT, it does not throw InvocationTargetException. To avoid the overhead of try/catch, this invokeAndWait() logs InterruptedException to the ErrorLog, which may not be what you want.


invokeLater

public static void invokeLater(java.lang.Runnable doRun)
This is a convenience method that just invokes SwingUtilities.invokeLater(). SwingRunner doesn't run the code now even if this is the EDT because that could unexpectedly change the sequence in which code is run.