Search code examples
javaswingswingworkerevent-dispatch-thread

Running function on EDT from doInBackground


I want to run a certain function from doInBackground on the EDT. I have it currently setup using publish and process which is working just fine. However, I want to know if there is a way to have a function run on the EDT from doInBackground without using publish and process. Also, without using invokeLater. Can I do this somehow?


Solution

  • You would do this as you would queue any code onto the EDT: via a Runnable that is added to the event queue:

       protected void doInBackground() throws Exception {
    
          // code to be called off of the EDT
    
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
    
                // code to be called on the EDT
    
             }
          });
          return null;
       }