Window Listener

windowOpened
windowClosed
windowClosing
windowIconified
windowDeiconified
windowActivated
windowDeactivated

Application : Application display another Frame and TextArea. When Frame is minimized, windowIconified( ) and windowDeactivated( ) method get invoked and simultaneously messages are displayed in TextArea. On maximizing Frame, windowActivated( ), windowDeactivated( ) and windowActivated( ) method are called and simultaneously messages are displayed in TextArea. On Closing the Frame, either you can call System.exit(0) which kill the entire application or call f2.dispose( ) method which can display windowclosing( )  and windowclosed( ) method.

 

import java.awt.*;
import java.awt.event.*;
class FrameWindow implements WindowListener
{
    Frame f1,f2;
    TextArea ta1;
   
    FrameWindow()
    {
        f1=new Frame("Frame 1");
        f2=new Frame("Frame 2");
        ta1 = new TextArea(20,20);
       
        //Change in Layout Manager if necessary
        f1.setLayout(new FlowLayout());
        f2.setLayout(new FlowLayout());
        f1.add(ta1);

        f2.addWindowListener(this);

        f1.setSize(200,200);
        f2.setSize(100,100);
        f1.setVisible(true);
        f2.setVisible(true);
    }
    public void windowOpened(WindowEvent e)
    {
        ta1.append("Opened\n");
    }
    public void windowClosed(WindowEvent e)
    {
        ta1.append("Closed\n");
    }
    public void windowClosing(WindowEvent e)
    {
        ta1.append("Closing\n");
        f2.dispose();
    }
    public void windowIconified(WindowEvent e)
    {
        ta1.append("Minimised\n");
    }
    public void windowDeiconified(WindowEvent e)
    {
        ta1.append("Maximised\n");
    }
    public void windowActivated(WindowEvent e)
    {
        ta1.append("Activated\n");
    }
    public void windowDeactivated(WindowEvent e)
    {
        ta1.append("Deactivated\n");
    }
    public static void main(String [] args)
    {
        FrameWindow x = new FrameWindow();
    }
}