What is the safe use of streams (event-dispatch) for JOptionPane.showMessageDialog and swing.utils.invokeAndWait?

I have a simple groovy script which from the main thread of execution should display some dialogs to the user.

My swing knowledge is limited and rusty, but I remember reading about the need to be careful to keep the GUI stuff in the event dispatch (EDT) stream.

If I just call the static JOptionPane.showMessageDialog method from my main thread, am I right to assume that this violates the correct practice of storing GUI files in EDT?

Should I use the swing.utils.invokeAndWait method, for example, in the following code example?

 void showHelloThereDialog() throws Exception { Runnable showModalDialog = new Runnable() { public void run() { JOptionPane.showMessageDialog( myMainFrame, "Hello There"); } }; SwingUtilities.invokeAndWait (showModalDialog); } 

Now the above does nothing to make values ​​from anything other than the message dialog that is available after invokeAndWait completes.

Presumably the fact that the groovy implementation "closes" Runnable will make the code simpler than above.

Is invokeAndWait required? And if someone, please give an example of the correct implementation to get the result of something like confirmDialog with groovy?

+6
java user-interface swing groovy event-dispatch-thread
source share
3 answers

Take a look at groovy.swing.SwingBuilder, it encapsulates invokeAndWait and invokeLater. Your example can be written as:

 import groovy.swing.SwingBuilder import javax.swing.* import java.awt.* def swing = new SwingBuilder() def myMainFrame = new Frame() swing.edt { JOptionPane.showMessageDialog( myMainFrame, "Hello There"); } 
+4
source share

Calling one of JOptionPane showXXXDialog () is LOCK until the user selects ok / cancel / etc. In general, you do not put such slow locks on the Dispatch Event (EDT) stream, as a rule, because every other GUI component will freeze. Thus, the gut instinct, so as not to put it on the EDT, is good, but it is also wrong. The reason is as stated by some others, the method creates GUI components, and this should always be done on EDT. But what about blocking? You will notice that even if you run it on EDT, it works great. The reason is inside the source code. The JOptionPane class creates a Dialog object and then calls show() and then dispose() , the first of which is to block the thread. If you read the comments (or javadoc), you will see what it says about this method:

If the dialog is modal and not yet visible, this call will not be until the dialog is hidden by calling hide or dispose. it is allowed to show modal dialogs from the event dispatch thread because the toolkit will start another event pump while the caller is blocked.

Thus, it is completely safe to run JOptionPane on EDT, despite the lock. Obviously, it is safe to call the Dialog show() method with EDT, but the same does not apply to JOptionPane , because its methods create GUI components, add listeners, access other containers when modal and block data entry, etc. You do not want all this to be done with EDT, because it is not thread safe, and there may be problems. Admittedly, I have never seen a problem using EDT's JOptionPane , and so the odds seem low, but they are certainly possible. Passing null for the dialog container and providing only immutable objects (for example, String s) as arguments for the fields will significantly reduce (perhaps even eliminate, as far as I know) the probability of something bad, because all the corresponding graphical interface components are created and accessible within the same stream until they are visible. But, you should just be safe and put it on EDT. It's not that hard to call SwingUtilities.invokeAndWait() .

+8
source share

It must be on EDT, so invokeAndWait or invokeLater is required. You can say that the JOptionPane.showMessageDialog code ultimately creates and modifies the Swing components. Starting with Java 6, Sun says that all manipulations with Swing components (whether they were implemented or not) should be performed on the EDT.

http://download.oracle.com/javase/6/docs/api/javax/swing/package-summary.html

http://www.velocityreviews.com/forums/t707173-why-does-jdk-1-6-recommend-creating-swing-components-on-the-edt.html

+5
source share

All Articles