• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to detect that a DOCUMENT_MODAL JDialog was closed?

 
Diego M. da Rosa
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

this is an issue brought up by the new JavaSE 6 Swing Modality API. Until JavaSE 5, it was easy to detect that a JDialog was closed simply by adding some code right after the call to JDialog.setVisible() (this call blocks the current thread, usually the Event Dispatch Thread):



Now, with the new DOCUMENT_MODAL modality type in JavaSE 6, it is possible to display two simultaneous modal dialogs, each one blocking one window hierarchy. The problem is that, when you close the first opened dialog, the EDT is not unblocked (it is kept blocked by the second dialog). The following code can be used to test this:



Scenario 1: the issue
1. Execute the program opening both windows;
2. Click the button on frame A;
3. Click the button on frame B;
4. Type a name on dialog A;
5. Close dialog A (frame A should be updated, but nothing happens!);
6. Type a name on dialog B;
7. Close dialog B (frame A and frame B are updated!).

Scenario 2: the order does matter
1. Execute the program opening both windows;
2. Click the button on frame A;
3. Click the button on frame B;
4. Type a name on dialog B;
5. Close dialog B (this time, frame B is updated normally!);
6. Type a name on dialog A;
7. Close dialog A (frame A is also updated normally!).

Scenario 3: the solution using WindowListener
1. Comment the code right below 'ALTERNATIVE 2';
2. Uncomment the code right below 'ALTERNATIVE 1';
3. Repeat the Scenario 1 (this time, frame A is updated right after the dialog is closed as expected).

So here is the question: what is the correct way to detect that a DOCUMENT_MODAL JDialog was closed? What do you think about the WindowListener approach?

Thanks in advance,
Diego
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a WindowListener.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Use a WindowListener.

My first thought as well.
 
Diego M. da Rosa
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies. As I said, using the WindowListener was also my first attempt.

It seems like a big change in the way people detect that a JDialog was closed. With the old modality style, adding code after setVisible() was clean and safe (JDK implementation of JOptionPane works like this). So, migrating from good old application modality to the new document modality will not be that simple.

Cheers,
Diego
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!