When a user clicks X on the window to exit, how do I set it up so that
a function is executed before the window exits ? Basically, this is to
give the user a "file not saved" warning before exiting.
Andrew T. - 12 Jul 2006 18:25 GMT
> When a user clicks X on the window to exit, how do I set it up so that
> a function is executed before the window exits ?
See
<file:///F:/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)>
HTH
Andrew T.
Andrew T. - 12 Jul 2006 18:28 GMT
> > When a user clicks X on the window to exit, how do I set it up so that
> > a function is executed before the window exits ?
>
> See
> <file:///F:/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)>
Or.. (polite cough) if you don't happen to be at my place, you might
try..
<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook(j
ava.lang.Thread)>
Andrew T.
Andrew T. - 12 Jul 2006 18:42 GMT
Uggh.. it was only after reading the replies of moiristo and Oliver
that I realised I had misunderstood your question.
Andrew T.
Moiristo - 12 Jul 2006 18:27 GMT
> When a user clicks X on the window to exit, how do I set it up so that
> a function is executed before the window exits ? Basically, this is to
> give the user a "file not saved" warning before exiting.
Create a WindowListener
Oliver Wong - 12 Jul 2006 18:29 GMT
> When a user clicks X on the window to exit, how do I set it up so that
> a function is executed before the window exits ? Basically, this is to
> give the user a "file not saved" warning before exiting.
Use a Window Listener to listen for the WindowClosing or WindowClosed
event:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/WindowListener.html
- Oliver
Steve W. Jackson - 12 Jul 2006 18:47 GMT
> When a user clicks X on the window to exit, how do I set it up so that
> a function is executed before the window exits ? Basically, this is to
> give the user a "file not saved" warning before exiting.
First, call this:
myFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Then add a WindowListener to your frame and implement its windowClosed
method. In that method is where you see if you need to save and respond
appropriately.
FWIW, you should probably go through the Swing tutorial Sun offers,
since it covers relatively common topics like this. And you may also
want to find the comp.lang.java.gui group, which is a better place to
post GUI-related queries.
= Steve =

Signature
Steve W. Jackson
Montgomery, Alabama
raphfrk@netscape.net - 14 Jul 2006 12:11 GMT
> First, call this:
>
> myFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Thanks, I just wanted to prevent people from closing without having to
save. Now, the menu "quit" option must be used to exit and I can put
the options on that.
Steve W. Jackson - 14 Jul 2006 19:51 GMT
> > First, call this:
> >
[quoted text clipped - 3 lines]
> save. Now, the menu "quit" option must be used to exit and I can put
> the options on that.
A well-behaved (make that "friendlier") application would still have a
listener that would simply invoke the "quit" item you refer to, so that
its action gets to the same code.

Signature
Steve W. Jackson
Montgomery, Alabama