Re: Self-configuring classes
On Aug 3, 10:58 pm, nebulou...@gmail.com wrote:
On Aug 4, 1:29 am, Owen Jacobson <angrybald...@gmail.com> wrote:
Because it does cause problems, but not consistently. As <http://
weblogs.java.net/blog/alexfromsun/archive/2005/11/
debugging_swing_1.html> observes, sometimes even simply creating a
window from main(String...) directly can have unexpected side effects.
Some obscure blog posting from a blog no-one reads. That's your
evidence against me?
What about the first google hit for "swing event dispatch thread":
<http://java.sun.com/docs/books/tutorial/uiswing/concurrency/
index.html>
That page lays out the threading responsibilities of Swing
programmers, and the *very next page* in the trail contains an example
snippet:
Sun posted:
You can see examples of this throughout the Swing tutorial:
SwingUtilities.invokeLater(new Runnable()) {
public void run() {
createAndShowGUI();
}
}"
That same example is the *second* link for the google search "java
event dispatch thread".
But let's say you don't know anything about event dispatch threads at
all! Let's say you're just getting started with Swing. Searching
google for "swing tutorial" gets you the Sun JFC/Swing tutorial at
<http://java.sun.com/docs/books/tutorial/uiswing/>. The inline
examples mostly use SwingUtilities; the few remaining examples of the
"old way" are slowly being corrected by Sun.
In JavaSE 6, a lengthy note was added to the API documentation for the
javax.swing package (<http://java.sun.com/javase/6/docs/api/javax/
swing/package-summary.html#package_description>) outlining how and why
Swing classes are thread-unsafe and how to use them correctly. It's
reasonable to expect developers to check the API docs for APIs they
use under new versions of the JDK sooner or later.
On Aug 3, 10:58 pm, nebulou...@gmail.com continued:
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
Eh, until you've presented some UI my understanding has been that the
EDT doesn't even exist yet; it's created lazily. So invokeLater might
be waiting a very long time, given that the UI waits for invokeLater
to invoke its argument before existing, invokeLater waits for the EDT
to exist to invoke its argument, and the EDT waits for the UI to exist
before starting...or has something in that department been changed? I
do hope it's not now true that all console apps however trivial
generate a useless EDT wasting memory and slowing down startup?
SwingUtilities' invokeNow and invokeAndWait methods both reliably
create and start the EDT if it isn't running, just like any GUI
component. This isn't as well-documented as it could be (you have to
read between the lines a bit in the API javadocs), but the tutorials
do make it very clear that this is what these two methods will do.
Also, ISTR SwingUtilities being a third-party (but commonplace) class,
not a standard Java class at all. Are you sure that one of the primary
Sun tutorials is assuming people have installed it, and not a third-
party tutorial or another of those blog postings?
The SwingUtilities class is in the package javax.swing, and is
provided as part of the Java Standard Edition platform at least as of
Java 1.3; you'd have to check the API docs yourself to see if it was
available earlier than that.