File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Acting as 'X' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Acting as Watch "Acting as New topic

Acting as 'X'

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Without accessing the actual code triggered by what the 'X' at the top right hand corner of a window does, is it possible to copy what it does? I can run a System.exit(0) which shuts down my application completely but when I click on the 'X' it gives a message before shutting down. I don't have access to the code that the 'X' triggers - yet I want to do the same thing.

Joined: Jan 07, 2002
Posts: 2
If I understand your question correctly, you are able to perform the function of 'X' from elsewhere except the messae it displays and now you want a mechanism to pop-up a message before your application is completely closed.
Well if my understanding is correct why don't you use a dialog box from javax.swing.JOptionPane right before you execute System.exit(0);
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Not exactly. It's not really the box itself that I'm interested in but whatever else goes on when a user presses the 'X'. Clearly it does more that just System.exit(0) so in effect what I want, is to call the same method that pressing 'X' calls. I don't have access to the code that does that but by using some other software I am able to attach listeners to the windows that are in focus.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

You may be able to do this... depending on how the class that extends Window that you are using handles the WindowClosing event...

If the event is being handled by adding a WindowListener to the window, you can get a reference to it via the getListeners() method of the Window class. (This is illustrated in the following code example.) However, if the window class you are using handles the window closing event through internal processEvent() or processWindowEvent()[/i] methods you are (most probably) not going to be able to directly call the method since these methods usually have a protected access level.

Anyway, hopefully, the class uses a WindowListener to handle the window closing. This technique will most likely work... all it does is get references to all the WindowListeners attached to the specified window... So if there are multiple WindowListeners attached to the window, you can't choose which one gets called. However, usually there is only one WindowListener (or at least only one that handles exiting, because the same behavior will happen in this case... there is no ordering of WindowListeners, so you can't be sure which will actually be called... ) The following code illustrates how to get a reference to the WindowListener and call it. P.S. - Be sure to take the &ls symbol out of the code and replace it with < in the for loop.

[ January 09, 2002: Message edited by: Nathan Pruett ]

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Acting as 'X'
It's not a secret anymore!