Get notification of a form being closed?

How can I recieve notification of a form being closed (in the class it's bound to) so that I can stuff some code in that event?

- Rex

5 comments
Comment actions Permalink

Some thing like that. I did not compile below codes. I did not write java codes for a while :)

0
Comment actions Permalink

Not exactly right but so dang close that Google debugged your pseudo-code perfectly and now it compiles just fine. :) The casual observer would hardly notice the differences.

Thank you very much!!!

frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

- Rex

0
Comment actions Permalink

An interesting problem though. If I'm in the middle of a for() loop that event will not fire even though the form is closed until after the loop is done. This is a problem as I really want the application to hammer shut if the user closes the form.

0
Comment actions Permalink

Well nevermind changing this from:
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

to

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Made that problem go away nicely. ;)

I'm beginning to love Java GUI programming.

0
Comment actions Permalink

Rex Winn wrote:

An interesting problem though. If I'm in the middle of a for() loop that event will not fire even though the form is closed until after the loop is done. This is a problem as I really want the application to hammer shut if the user closes the form.


The Java Swing GUI framework operates on the principle of a single
thread which processes all GUI events. If in response to an event (like
clicking a button on a form) you perform some long process, the GUI will
be blocked and appear to hang because it is unable to process events
until your long process finishes.

Processes like that should be split off into a separate thread. Bear in
mind though that any changes to the GUI that this process makes must be
made on the Swing thread. You'll need to use the SwingUtilities class'
invokeLater or invokeAndWait methods to make GUI changes from another
thread.

If you wish to be able to stop a long-running process in a separate
thread in response to a GUI event, you will probably need some sort of
flag that the long-running process periodically checks that can be set
by the GUI event handler.

Ciao,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://www.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: (416) 933-5046 | Fax: (416) 933-5001

0

Please sign in to leave a comment.