this.setDefaultCloseOperation ( JFrame. DO_NOTHING_ON_CLOSE ); // what happens when user closes the JFrame. WindowListener windowListener = new WindowAdapter() { // anonymous WindowAdapter class public void windowClosing ( WindowEvent w ) { // Whatever application code you want to do on close, e.g. rememberLocation( MyFrame.this.getX(), MyFrame.this.getY() ); // Whatever code you want to actually close the JFrame, e.g. MyFrame.this.setVisible( false ); MyFrame.this.dispose(); } // end windowClosing };// end anonymous class this.addWindowListener( windowListener );