This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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. Paul
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);
Joined: Mar 15, 2000
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.
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.