As a rule of thumb all Swing GUI operations should be done on the AWT Event Dispatch Thread (EDT). This implies that you must make all alterations to the GUI itself “cheap” so that the GUI doesn't block when you modify it, and that long running tasks should be delegate to worker threads or something similar. In particular you should not perform I/O on the EDT.
The typical way of ensuring that your GUI operations occur on the EDT --regardless from which thread the GUI update is requested-- is to schedule a new job in the AWT EventQueue:
Code:
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// change text in myJLabel. Must be a JTextComponent, obviously.
myJLabel.setText("hello world!");
}
});
Bookmarks