Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Save on exit

Thread view: 
raphfrk@netscape.net - 12 Jul 2006 18:08 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.
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.