I have some modal dialogs (created using JOptionPane.showXXXDialog methods) that I need to replace with non-modal dialogs. The dialog is already running in a separate thread (it is kicked off by an asynchronous event) but I need the user to have access to other parts of the GUI. On the other hand, since the dialog is to be presented in response to an asynchronous event, I need to know precisely when the dialog goes away, because the response to the dialog is necessary to handle the event.
For some reason I can't find very much information on non-modal dialogs. Can anyone help?
Joe: That creates a modal dialog not a non-modal dialog.
Robert: It would appear to me that you understand how to create a non-modal dialog and are simply confused as to how you determine when that dialog has closed. I believe the simplest solution is simply to use a WindowListener. java.awt.Window supports WindowListenerS and unless I'm mistaken the JDialog should send out the appropriate windowClosed(WindowEvent) when it closes. Thus rather than doing something like
you would instead do something like
Obviously that's pseudo code but you get the idea. If for some reason a WindowListener doesn't suit your needs you could take a step further and make your own JDialog that accepts a listener of some sort and sends out a notification that the user is done with it at the appropriate time. Then implement a listener to do the action you need to do when that happens and register it (as I did with a WindowListener).
If you want help with your specific implementation please post more. Are you subclassing JDialog? Using a JOptionPane convenience method? Encapsulating a JDialog within another class? By the sounds of things you'll be able to figure it out from what I mentioned, but if you need extra help post more details. Thanks. [ September 12, 2005: Message edited by: Ken Blair ]
Originally posted by Ken Blair: Joe: That creates a modal dialog not a non-modal dialog.
According to the JDialog API documentation , the two argument (Frame, String) constructor creates a non-modal dialog. However, the code, which I copied verbatim from the JOptionPane documentation, is structured for handling a modal dialog. You are correct in steering Robert to use a WindowListener or custom event.
Yes, the two argument constructor creates a non-modal dialog. Unfortunately, the code copied from JOptionPane doesn't use a constructor and instead uses JOptionPane.createDialog(component, string). The JOptionPane documentation could use some work as it doesn't bother to specify whether or not the dialog is modal, but I compiled a quick version to check and it was. No surprise of course since that code wouldn't work with a non-modal dialog.