Re: File browser
Peter Duniho wrote:
On Tue, 05 Feb 2008 17:05:27 -0800, Daniel Pitts
<newsgroup.spamfilter@virtualinfinity.net> wrote:
If you don't know what the EDT is, then you are likely doing something
wrong. I suggest looking at the sun tutorial on Swing and threads.
<http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html>
Hmmm. Well, that article seems to say that my code is perfectly fine.
It specifically calls out initialization as something that's okay to do
in the main thread.
Odd. Recently Sun has been saying that they were mistaken, and that indeed
even initialization must happen only on the EDT.
<http://java.sun.com/docs/books/tutorial/uiswing/concurrency/dispatch.html>
Some Swing component methods are labelled "thread safe" in the API specification;
these can be safely invoked from any thread. All other Swing component methods
must be invoked from the event dispatch thread. Programs that ignore this rule
may function correctly most of the time, but are subject to unpredictable errors
that are difficult to reproduce.
What, even construction and layout?
Yep - check out their own example:
<http://java.sun.com/docs/books/tutorial/uiswing/examples/components/TopLevelDemoProject/src/components/TopLevelDemo.java>
Note that they take great care to "create and set up the window" in a
createAndShowGUI() method that is invoked, via SwingUtilities, explicitly on
the EDT, not from the main() thread. (Check out the very Java-idiomatic and
nifty use of an anonymous-class implementation of Runnable.)
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
Yes, Virginia, there is an EDT clause even for construction and initialization
of your GUI.
--
Lew