Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwingUtilities.invokeLater() why is it needed?

Tags:

java

swing

Why is it necessary to put GUI update code in SwingUtilities.invokeLater()?

Why cant it be internally taken care of by Swing itself? Why does the caller have to care about how swing handles UI updates?

like image 438
pure.java Avatar asked Aug 23 '10 20:08

pure.java


1 Answers

Swing objects are not thread safe. SwingUtilities.invokeLater() allows a task to be executed at some later point in time, as the name suggests; but more importantly, the task will be executed on the AWT event dispatch thread. When using invokeLater, the task is executed asynchronously; there's also invokeAndWait, which won't return until the task has finished executing.

Some information about the decision not to make Swing thread-safe can be found here: Multithreaded toolkits: A failed dream? [Archived]

like image 57
Richard Fearn Avatar answered Sep 27 '22 23:09

Richard Fearn