File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How is modality implemented? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How is modality implemented?" Watch "How is modality implemented?" New topic
Author

How is modality implemented?

marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Hello,

If you call setVisible(true) on a modal dialog, code subsequent to this instruction will not run until the dialog is disposed and I'm wondering how is this done.
How do you prevent the EDT from proceeding with no other thread to notify it? And if the EDT is blocked, where does event handling for the dialog occur?

Thanks.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Well, this is a guess, but I'm assuming that the EDT isn't blocked, but the parent of the modal dialog simply ignores events. To see how it is implemented, see the source code for JDialog.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

I just took a look at the code, and what happens, more or less, is the following:

When you show a modal dialog, it will block the "real" event dispatch thread, but it will start a new thread which deals with the event queue. This new thread will have a filter applied on it, which causes it to block events for the owner of the modal dialog, but deals with other events as it would normally.

When you dismiss the modal dialog, the new thread will stop, and the real event dispatch thread will continue running.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

And since this is in Java in General and not AWT/Swing etc, it's worth mentioning that this behavior isn't followed by JavaFX 2.0.
Showing a modal Stage doesn't block the application thread


luck, db
There are no new questions, but there may be new answers.
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Hi Stephan thanks for your response. I suspected something like this. I'd like to see how this is written but I can't find where in the hierarchy this is implemented. Would you please tell me what method in what class did you look at?

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Check out the show() method of java.awt.Dialog.
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How is modality implemented?