Author
catching the event before a java program will end . . .
Sanjeev Charla
Ranch Hand
Joined: Apr 02, 2009
Posts: 90
i am not in a gui program to do something before window is closing . . .
http://www.rgagnon.com/javadetails/java-0459.html show me, how to catch the sudden cancellation of a java program.
but how to know before, when a jvm is exiting normally??
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted Aug 20, 2010 01:27:00
0
not sure I'm reading this right, but one of the ways is to set the frame's closeOperation to DO_NOTHING_ON_CLOSE,
then add your own windowListener, and in windowClosing do what you want before calling System.exit(..)
Peter Taucher
Ranch Hand
Joined: Nov 18, 2006
Posts: 174
A window listener would react on behalf of the window. Some applications don't necessarily run in window mode. A shutdown hook would always be possible (depends on your targets/presets):
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29
http://cupi2.uniandes.edu.co/site/images/recursos/javadoc/j2se/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29
Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
Shutdown hooks are indeed the way to go. They will only not be called if the JVM is terminated forcefully (e.g. using Runtime.getRuntime().halt(), or by killing the process from the operating system)
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
This is probably a more helpful link about shutdown hooks; note it is a bit old however. Another (also old) link .
Shutdown hooks are nice things
subject: catching the event before a java program will end . . .