Closing a Swing Frame, user vs system initiated close. How to tell the difference?
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开发者_StackOverflow社区 or HIDE_ON_CLOSE and I know how to capture the event. All I need is "who" initiated it.
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.
精彩评论