Save on exit

R

raphfrk

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.
 
A

Andrew T.

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.
 
M

Moiristo

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
 
A

Andrew T.

Uggh.. it was only after reading the replies of moiristo and Oliver
that I realised I had misunderstood your question.

Andrew T.
 
S

Steve W. Jackson

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 =
 
R

raphfrk

Steve said:
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.
 
S

Steve W. Jackson

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top