Notifying exit
My main class which extends JFrame has an Exit button with an ActionListener to exit the program.
This class calls a method in another class which is doing things with files. I want to notify this method that Exit has been clicked so that it can close open files and clean up etc. before the interrupted program exits (or in some circumstances maybe prevent the exit happening at all).
Very newbie question, I know, but how do I do this?
[456 byte] By [
JeffG1a] at [2007-11-27 2:11:22]

In the class you are calling can you not just add a method called shutdown which does any cleanup work and if successful returns true and if not it returns false, in your calling code you can call system.exit if shutdown in the other class returns true or do otherwise if false.
// on exit button pressed
if(OtherClass.shutdown()) {
System.exit(0);
}else {
//Do whatever needs to be done
}
Message was edited by:
java1981
If you want to "veto" a window closing: first, don't have the window close by default:
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
and second, have a window listener react to the windowClosing event. It will decide whether or not to really close the window.
Thanks for the helpful responses. The shutdown method approach is simple and obvious (with hindsight) and should achieve what I want.
> If you want to "veto" a window closing: first, don't
> have the window close by default:
> > frame.setDefaultCloseOperation(WindowConstants.DO_NOTH
> ING_ON_CLOSE);
>
> and second, have a window listener react to the
> windowClosing event. It will decide whether or not to
> really close the window.
Yes, I already have something similar, thanks, so that clicking on the X runs the Exit button code:
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
--
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
buttonExit.doClick();
}
} ) ;