This tip shows the way to add window listener to a Frame. This listener is used when certain operations need to be done at different life cycle points of the window. For example if you want to save the data or you want to free the resource before closing of the window. This you can do in the windowClosed() method.

 import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;

public class WinLsnrExp extends JFrame {
    public WinLsnrExp() {
        setTitle("Window listener Example : ");
        setSize(150, 150);
    public static void main(String[] args) {
        WinLsnrExp wle = new WinLsnrExp();
        wle.addWindowListener(new WindowListener() {
            public void windowClosed(WindowEvent arg0) {
                System.out.println("Window close event occur");
            public void windowActivated(WindowEvent arg0) {
                System.out.println("Window Activated");
            public void windowClosing(WindowEvent arg0) {
                System.out.println("Window Closing");
            public void windowDeactivated(WindowEvent arg0) {
                System.out.println("Window Deactivated");
            public void windowDeiconified(WindowEvent arg0) {
                System.out.println("Window Deiconified");
            public void windowIconified(WindowEvent arg0) {
                System.out.println("Window Iconified");
            public void windowOpened(WindowEvent arg0) {
                System.out.println("Window Opened");