4

When the user clicks the x to close the frame, I want to take one action. When Windows shuts down and triggers a close however, I wish to take a different action. Naturally, I will be using DO_NOTHING_ON_CLOSE or HIDE_ON_CLOSE and I know how to capture the event. All I need is "who" initiated it.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
R Hughes
  • 640
  • 9
  • 22
  • [`WindowListener`](http://stackoverflow.com/questions/2141325/q-about-java-windowlistener/2143965#2143965)? See also [How to Write Window Listeners](http://download.oracle.com/javase/tutorial/uiswing/events/windowlistener.html). – trashgod Sep 23 '11 at 02:22
  • I've implemented WindowListener, but the WindowEvent sent during WindowClosing does not seem to give me what I need. I'll poke around in the tutorials you sent though and see if I'm just missing something not commonly used. – R Hughes Sep 23 '11 at 02:26
  • Worse yet, in Win7, when the system tries to shutdown and I don't allow close on exit, the whole OS sits on it's thumbs waiting for my program to finish. It's most annoying. I'll look into the shutdown hook, thx. – R Hughes Sep 23 '11 at 03:03
  • Here's a related approach used on [Mac OS X](http://stackoverflow.com/questions/2061194/swing-on-osx-how-to-trap-command-q/2061318#2061318). – trashgod Sep 23 '11 at 03:08

1 Answers1

4

A WindowListener will tell you when the user did something that affects a window, but abrupt termination of the JVM may produce no window events at all. You may be looking for the Runtime method, addShutdownHook(), discussed here and here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    This looks very promising. I'll do a little reading up and try it out. If it works out, I'll gladly accept the answer. – R Hughes Sep 23 '11 at 03:01
  • No go, even with CLOSE_ON_EXIT setup. It looks like I may have a much bigger issue than I thought. When Win7(64bit) tries to shutdown, my JVM goes unstable. The program ceases to function on any level. I can only close it from the task bar and javaw.exe gets left behind in task manager. Windows will not shutdown till that is killed and it does not even tell you why. Looks like I'll need to work that issue out before I can even address this one. But, that's a different thread... – R Hughes Sep 23 '11 at 04:50
  • Ouch! Any daemon threads or un-`dispose()`d host resources? See also [§12.8 Program Exit](http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.8). – trashgod Sep 23 '11 at 09:51
  • I'll check into it, but I just cross checked with another program written by a completely different company and the exact same thing happens to it. I think I need to start looking much further down the rabbit hole. Starting with javaw.exe – R Hughes Sep 23 '11 at 15:02
  • Well I've solved part of the mystery. I hadn't null-ed out my properties file and javaw was hanging onto resources. Research tells me not to trust shutdown hooks to run consistently. Testing seems to bear that out. Also, I found that both window closing and shutdown hooks run when user clicks X. But, I've since redesigned so that I don't care how the program is closed. However, I'm guessing a close event in conjunction with a mouse event might be the ticket. Time for more research. – R Hughes Nov 04 '11 at 00:07
  • Thank you for the followup; I'd welcome your answer, as results accumulate. There are `dispatchEvent()` examples [here](http://stackoverflow.com/questions/7456227/how-to-handle-events-from-keyboard-and-mouse-in-full-screen-exclusive-mode-in-ja/7457102#7457102) and [here](http://stackoverflow.com/questions/5540354/java-swing-the-right-action-to-take-upon-closing-windows/5540802#5540802). – trashgod Nov 04 '11 at 03:54