Java tutorial
//package com.java2s; import java.awt.Window; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Main { /** * Attaches a {@link WindowListener} to the given Window so it is disposed * when the close button is hit. Some windows do not respond to the * DISPOSE_ON_CLOSE operation, so this is better than using that. */ public static void attachDisposeOnClose(final Window w) { w.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { w.setVisible(false); w.dispose(); } }); } }