Issue
I have a SWING
form and when I close the application I want to save data to a text file.
This is the code generated by Swing which I cannot modify:
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosed(java.awt.event.WindowEvent evt)
{
formWindowClosed(evt);
}
});
Here is my custom implementation of formWindowClosed
. The problem is that the app closes without executing formWindowClosed.
Isn't formWindowClosed
the method where I want to put the code that gets executed before the app closes ?
formWindoClosed
implementation:
private void formWindowClosed(java.awt.event.WindowEvent evt)
{
System.out.println("Message");
brain.getBirou().getProbaRepo().WriteParticipantToTXT();
}
Solution
If you want to execute code before the process exits, you have to set the default close operation to WindowConstants.DISPOSE_ON_CLOSE
then in the listener method save your file and then call System.exit(0);
when you're done.
Also "Swing" doesn't generate code by itself. Maybe you should write your GUI code for your self, by hand. Then of course you can modify anything.
You have to have the possibility to change exit behavior of your window, so setting it to dispose on close should help. Maybe you should look up where you can change it in your IDE. (Or just open it with a text editor...)
Answered By - Zhedar
Answer Checked By - David Goodson (JavaFixing Volunteer)