devi associare un'evento al tuo frame

codice:
//.....
frame.addWindowListener(new WindowAdapter(){
  
  public void windowClosing(WindowEvent e){
     //DO YOUR STUFF HERE
  }
});

//...