Java JFrame handle window event
import java.awt.FlowLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; public class Main extends JFrame { public Main() { super("JButton"); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout()); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("about to close"); }//from ww w . j a v a 2 s. co m }); } public static void main(String[] args) { Main frame = new Main(); frame.pack(); frame.setVisible(true); } }