Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Acting as 'X'

 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shewitti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


-Nate
[ January 09, 2002: Message edited by: Nathan Pruett ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic