Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How is modality implemented?

 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6320
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6320
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6320
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the show() method of java.awt.Dialog.
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic