How do I feel the essentialness of SwingUtilities.invokeLater() in any swing application.Please give some code example.
+3
A:
Whenever you need to update something inside your GUI you should do it through the AWT Event Thread.
This because AWT (and Swing on top) has its own thread that manages everything of a GUI. Without it the graphical interface couldn't handle events and similar things in an asynchronous way while your program is doing something else.
So for example if you have a long task declared in a Thread
:
public void MyThread extends Thread
{
class GUIUpdate implements Runnable
{
GUIUpdate(String msg)
{
...
}
public void run()
{
guiElement.appendText(msg);
}
}
public void run()
{
while (finished)
{
//do long calculations
//send partial output to gui
SwingUtilities.invokeLater(new GUIUpdate("something has changed!"));
}
}
}
Jack
2010-07-02 01:00:30