I’m trying to call a function to do cleanup when my window (created with Java Swing) is closed .
In my initialization code I do this:
public class FormLogin extends JFrame{
private void initComponents(){
...
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent evt){
formLoginWindowClosed(evt);
}
});
...
}
}
But the function “formLoginWindowClosed” is never called when I press the exit button. I’ve also tried creating the listener with java.awt.event.WindowAdapter as an argument, but it didn’t work either.
How should I create the listener for window closing?
Thanks in advance.
With the frame set to exit on close,
windowClosedwill never be called, mostly because the system has already exited before the event can be raised.Try using
windowClosinginstead.Alternatively, you could use a
shut down hook