How to determine when a JFrame opened or closed


*******************************************************
COMPLETE SOURCE CODE FOR : DetermineWhenAJframeIsOpenOrClose.java
*******************************************************


import javax.swing.JFrame;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class DetermineWhenAJframeIsOpenOrClose
{
JFrame frame;

JFrame a=new JFrame("OPEN A JFRAME WITH TITLE'S MONSTER");
JButton b=new JButton("CLICK HERE TO OPEN");

public DetermineWhenAJframeIsOpenOrClose()
{

b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
frame=new JFrame("MONSTER");
frame.setSize(300,500);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter()
{
//windowOpened METHOD WILL BE CALLED WHEN A JFRAME IS OPENED
public void windowOpened(WindowEvent evt)
{
JOptionPane.showMessageDialog(null,"MONSTER IS OPEN");
}

//windowClosing METHOD WILL BE CALLED WHEN A JFRAME IS CLOSING
public void windowClosing(WindowEvent evt)
{
Frame temp=(Frame)evt.getSource();
temp.dispose();
JOptionPane.showMessageDialog(null,"MONSTER IS CLOSE");
}

});

}
});

a.add(b);

a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(500,100);
a.setVisible(true);

}

public static void main(String[]args)
{
DetermineWhenAJframeIsOpenOrClose mainObject=new DetermineWhenAJframeIsOpenOrClose();
}
}


*******************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************

RELAXING NATURE VIDEO