Reputation: 34820
We have a Swing application with socket communication and other threads. We know we need to use SwingUtilities.invokeLater()
to update Swing display from other threads. But how about JOptionPane
? do we need to use SwingUtilities.invokeLater()
from other threads to launch JOptionPane
?
if the answwer is yes, how to use JOptionPane to block the procedure?
if the answer is no, doesn't JOptionPane belong to GUI display?
Upvotes: 2
Views: 2532
Reputation: 36621
if the answwer is yes, how to use JOptionPane to block the procedure
The answer is indeed yes. You should access/modify/... all Swing components on the Event Dispatch Thread. So your background thread needs to use some mechanism to call the JOptionPane
on the EDT. Using SwingUtilities#invokeLater
is an option, but not a blocking one.
Use SwingUtilities#invokeAndWait
for the blocking behavior. This method will wait until the runnable is finished before returning.
Upvotes: 3
Reputation: 99
The answer is yes, you have to call swing components only from the EventQueue. Have a look at Is JOptionPane.showMessageDialog thread safe?
Upvotes: 2
Reputation: 6475
Yes, you need to use SwingUtilities.invokeLater()
to launch the JOptionPane - otherwise the JOptionPane may not be responsive. Since it's a modal container, it should block input to the main container by default. See http://docs.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html for more details.
Upvotes: 2