Stories, software, and a life lived across several worlds
While reading Improve Application Performance With SwingWorker in Java SE 6 from the Sun Developer Network I found that I made the most common mistake in Swing programming myself as well.
All Java programs start with an initial thread out of the main() method. A lot of people - including myself - will create the application main window as a JFrame there in main() and show the UI. That's wrong!
The correct way is to use the Event Dispatch Thread (EDT) for all user interface related stuff. So creating the application main window outside of the EDT is wrong, because it might lead to thread synchronization problems later. The right way to do it is to use the invokeLater() method from SwingUtilities:
public class MainFrame extends javax.swing.JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
}
Doing so has another advantage. The call to invokeLater() returns immediately allowing your application to perform other initialization tasks without delay. If a wait is required, one can use invokeAndWait() instead.
| Previous | 24 Jan 2007 | Next |
About me
Hello! My name is Stephan Schwab.
I build and rescue software, and I write fiction about the human side of how it gets made. Here you’ll find my stories and novelas, notes on craft, and field notes from a life lived across several worlds.
Working with software teams is what I do professionally — see how on caimito.net. You can also read about my experience since 1986.
Work with me
Hire me as the senior who embeds in your team and makes it ship.
Stories & writing
On the craft
Life across several worlds
Places that shaped me
Open Source
Stay in touch
LinkedIn Mastodon Bluesky TikTok Twitter RSS Email
Everything
See a listing of all posts on this site.