GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Forcibly closing all open dialogs on an Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Forcibly closing all open dialogs on an Applet " Watch "Forcibly closing all open dialogs on an Applet " New topic
Author

Forcibly closing all open dialogs on an Applet

Sabhtarsha Kore
Greenhorn

Joined: Jun 22, 2005
Posts: 3
Hi All,
Had a query-- if we can forcibly close all the open dialogs on an Applet. Googled about it but did not get any easy way to do it.

Actually I want to do this because my applet can logout due to many reasons, one of which is network failure, which can occur to a background thread, in this case if a modal dialog is visible on the applet it would remain visible whereas the applet below can log itself out and start showing the login back screen. So I would like the applet to close all the visible dialogs when it logs out. (due to any reason)

It would be great even if we get a collection of all dialogs opened over an applet, we can easily call dispose on them.
Found a way to get this list, below is the pseudo code, but I am not convinced in doing it this way.

------------------------------------
Field fieldModalDialog = Dialog.class.getDeclaredField("modalDialogs");
Collection modalDialogs = (Collection) fieldModalDialog.get(dialog);
for each modalDialog in modalDialogs{modalDialog.dispose);}
------------------------------------
But again this would give us a list of modalDialogs.. I have'nt searched any thing for non-modal dialogs..

Does anyone know a way better than this?

~sabhtarsha~
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
Welcome to JavaRanch.

The applet should keep references to all the windows/dialogs that it opens; then it can just call their close() method whenever appropriate.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Shouldn't that be setVisible(false) or dispose()?
Either way, just make sure to do it on the Event Dispatcher Thread. You can use EventQueue.invokeLater if needed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
Oops, quite right, dispose() is what I meant.
Sabhtarsha Kore
Greenhorn

Joined: Jun 22, 2005
Posts: 3
Well, thanks guys for your replies.
I wanted some other way to get the list of open dialogs on a applet or a window, is'nt there any method or some way we can retrieve it. I am sure they (or looking at the source code they _do_) keep the list of open dialogs. Its just that they have'nt shared it through an API.

Any ways.. thanks for trying, as the dialogs in my application are used with a wrapper on JDialog its easy for me to maintain this list and flush the open dialogs at any time. But if this would not have been the case then it would have been difficult ....

Thanks,
~sabhtarsha~

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Since Java 6, java.awt.Window has a method called getWindows() which
Returns an array of all Windows, both owned and ownerless, created by this application. If called from an applet, the array includes only the Windows accessible by that applet.

Warning: this method may return system created windows, such as a print dialog. Applications should not assume the existence of these dialogs, nor should an application assume anything about these dialogs such as component positions, LayoutManagers or serialization.

You would need to iterate over it and filter out the dialogs:
Sabhtarsha Kore
Greenhorn

Joined: Jun 22, 2005
Posts: 3
Thanks Rob
and Thanks to 1.6
It works.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forcibly closing all open dialogs on an Applet