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 |
This article has been posted to social media sites. There might be comments. Just follow the links:
About me
Hello! My name is Stephan Schwab.
As International Software Development Coach and Consultant I help CEOs and Department Leaders to improve value creation and cohesion within their organization. The outcome will be higher quality, customer delight and more revenue.
Learn about my professional experience since 1986.
Professional Services
I'm fluent in these human languages:
Scrum Pair-Coaching to develop technical competence:
Resources for new clients:
Search
Special Content
Highlights of the Year
Living on planet Earth
Open Source Projects
Stay in touch
My Books
Everything
See a listing of all posts on this site.