Submitted On 18-MAR-2002
vk1963
JOptionPane also has the same problem when showing a modal
dialog in JDK 1.4 final. This is so basic functionality
that is used often in Swing applications. How could this
have been missed?
Submitted On 05-APR-2002
greggwon
All you have to do is use SwingUtilities.invokeAndWait()
around the dialog setVisible(true). It is extremely
frustrating for sure...
Submitted On 19-APR-2002
dave_nedde
invokeAndWait will not work from the event thread, if you
have dialog code that can be called both from the event
thread and from a non-event thread. invokeLater will work
in either case.
/** Display an error dialog or print a message to stdout. */
public void showError (String title, String text)
{
class SafeMessageDialog implements Runnable
{
Component parentComponent;
Object message;
String title;
int messageType;
public SafeMessageDialog( Component
parentComponent,
Object message, String title, int
messageType )
{
this.parentComponent =
parentComponent;
this.message = message;
this.title = title;
this.messageType = messageType;
}
public void run()
{
JOptionPane.showMessageDialog(
parentComponent, message,
title, messageType);
}
};
// Works around Java Bug 4636311: Hang when showing
a modal dialog
// from a Runnable.
// Workaround - Use invokeLater() around the dialog
setVisible.
SwingUtilities.invokeLater( new SafeMessageDialog(
null, text, title,
JOptionPane.WARNING_MESSAGE ) );
}
Submitted On 28-APR-2002
Hedin
Dialog hangs because of wrong parent. If you pass the right
one, then problem should gone. However determinig the right
parent can be difficult in some situation. There is an RFE
4466805 to improve this situation.
Submitted On 22-MAY-2002
frito
Hello,
there is a somewhat related Bug report concerning freezing
of JFileChooser boxes under sdk 1.4 with BugID 4479139. I
had that problem with freezing, too.
After having read the information given here for BugID
4636311 I came back to my code with the JFileChooser. I
recognized that I had used that very JFileChooser object in
a syncronized() statement to prevent concurrent access in a
multi-threaded, event-thread based environemt. I changed
the object to synchronize on from the file chooser object
to an unrelated string, and ... it worked !
Synchronization on the file chooser object was totally OK
with sdk 1.3.1_02, but froze the dialog under sdk 1.4.0_b92
which I'm currently using. I don't know whether that is the
general solution to one or both of the Bugs reported, but
with me it's OK.
Best regards,
Kurt Spanier
Submitted On 03-JUN-2002
torsten_welches
I am puzzled that the evaluation says this was a bug?! The
provided snippet violates Swing's single-thread rule, hence
the problem.
JOptionPane.showMessageDialog needs to be called on the
event-dispatch thread (SwingUtilities.invokeLater) and the
problem disappears.
Or what am I missing?
Submitted On 06-JUN-2002
PaulFranz
I agree with torsten. This is violating the Swing
single-thread rule and therefore should be ignored.
Submitted On 15-JUN-2002
vy_ho
torsten has good point. Although this was originated in the
event, it actually called in a separate thread than the
current action event.
The code already has a runnable, just invokelater and that
would do.
Submitted On 28-JUN-2002
WördehoffH
I am not sure if this is really violating the single thread rule of Swing. The rule states that no calls may be
made from another thread _after the Window/Dialog has been realized_, i.e. after the dialog is packed/show.
Therefore creating and showing the dialog the way the example does should be valid.
Maybe JOptionPane does some things the wrong way round so that it gets bitten by the single thread rule.
But then JOptionPane is broken and not the sample code.
Submitted On 10-JUL-2002
cairn
WördehoffH is right, this *isn't* violating the thread
rule (because you're allowed to call show() / setVisible
(true) from non event-threads).
/However/, the main (and only) reason this is hanging is
because of this:
JOptionPane.showMessageDialog(null,
^^^^
This isn't just a 1.4 issue, using a null parent when
invoking a modal dialog on a thread other than the event
thread causes this behavior in 1.3 too.
Simple fix: supply a valid parent for the dialog. It's
good practise to do this anyway.
Submitted On 18-JUL-2002
torsten_welches
Swing's single-thread rule states:
"Once a Swing component has been realized, all code that
might affect or depend on the state of that component
should be executed in the event-dispatching thread."
Opening a Dialog clearly *does* change the state of an
already realized frame, like grabbing the input focus. Hence
my understandig is that you are allowed to realize a top-level
component on a non-AWT (typically the main) thread *only
once* - the first one that actually starts the event dispatch
thread. Everything else has to be realized on the event
dispatch thread.
Again, this is how I understand the single-thread rule. I might
be wrong.
I certainly agree that providing a dialog parent is good
practice anyway ;-)
Submitted On 31-JUL-2002
schlm3
Agree with Thorsten / cairn. This is not a Bug.
However, I come to this bug while searching for a (possible)
Bug I found out. When trying to show the modal Dialog during
a WindowActivated event of MyDialog, the whole Application
hangs! Showing the dialog with invokeLater solves the
problem, however, this is not a threading-problem.
Will file this as a new Bug.
Submitted On 03-AUG-2002
alflanagan
Duplicated on Mandrake Linux 8.2, KDE 3, J2SDK 1.4.1beta.
Submitted On 06-AUG-2002
se95sn
Is this related to the drag and drop bug? If you try displaying
a modal dialog before completing a drop operation you get a
busy hang....
Submitted On 10-OCT-2002
vy_ho
I seems to see what *schlm3* above seem. With the lastest
JDK 1.4.1 FCS, the problem is still there. The 1 thread
rule does not seem to be violated. Changed to invokeLater
worked. But it should be a bug. I'll see if I need to file
a bug report on this.
Submitted On 11-DEC-2002
cairn
torsten_welches:
yes, however, Dialog (which JDialog inherits from, and does
not override show()) is NOT a swing component, it's a
heavyweight peer. So the thread rule for swing does not
apply. The javadoc for Dialog.show() actually says you can
invoke it from any thread (albeit in a subtle and slightly
misleading way).
Any focus change as a result of showing the dialog would be
handled by the focus manager, and this would always be in
response to a generated AWT event - i.e. not in the same
thread you called show() from. So, it's not a swing thread
safety issue at all.
If you're still not convinced, consider this... If you
wanted to write an application that was solely dialog based,
the very first thing you'd do in your main() method would be
to call show() on a (J)Dialog instance you constructed.
However, your main() method is NOT on the AWT event thread,
because your application has not invoked any UI at that
point. If you were not able to call show() from threads
other than the AWT event thread, you would never be able to
safely show a dialog in such an application (actually, this
applies to frames too, so you'd never be able to display a
graphical application period).
Submitted On 12-DEC-2002
echawkes
The single-thread rule applies to both AWT and Swing: unless
the docs
for an API specifically state that it is thread-safe, it is
not thread-safe.
However, cairn is right: it is permissible to initialize the
GUI on the
main thread. To continue modifying the GUI on the main
thread after
initialization is not supported.
Submitted On 23-MAR-2003
greggwon
I use the following method:
public static void runInSwing( Runnable r ) {
if( SwingUtilities.isEventDispatchThread() ) {
r.run();
} else {
try {
SwingUtilities.invokeAndWait( r );
} catch( InterruptedException ex ) {
}
}
}
I wonder if we can get something like this put into
SwingUtilities?
Submitted On 02-APR-2003
yooj101
This bug prevents us from creating a modal dialog that shows
the progress of uploading files. The dialog's components are
painted only when the action finishes. This defeats the whole
purpose of the progress monitor. Please fix this.
PLEASE NOTE: JDK6 is formerly known as Project Mustang
|