Search code examples
javamultithreadingswinguser-interfaceevent-dispatch-thread

Should i call InvokeLater inside a thread created in the EDT?


what happens if i modify my gui inside a thread created in the edt?

When i say "created in the edt" i actually say that the thread declaration and its starts are in a block of code surrounded by:

SwingUtilities.invokeLater(new Runnable(){

   public void run(){

      Thread t= new Thread(new Runnable(){

         public void run(){

            txtField.setText("setting...");
         }
      });

      t.start();
   }
}

will this modify my gui without side effects? or should i call again invokeLater inside Thread "T"?


Solution

  • It doesn't matter whether you instantiate and run the thread in EDT. It will be a new thread, running independently from EDT, and therefore it will not be safe to modify the GUI from it. All Swing modifications must be done using invokeLater().