Way2Java

Java Frame Close WindowListener

The frame created by the programmer and displayed on the monitor comes with a title bar and three icons(buttons) on the title bar – Minimize, Maximize and Close. The first two works implicitly and the Close does not work and requires extra code to close the frame.

Java Frame Closing – 4 styles

Four styles exists to close the frame and are discussed one-by-one in this tutorial.

  1. Using WindowListener
  2. Using WindowAdapter
  3. Using Component
  4. Using Anonymous inner class

The last style is used very often as it requires less code to write. Now let us see how to close the window using the listener.

Java Frame Close WindowListener

In the following program, WindowListener interface is implemented to close the frame. The seven abstract methods of WindowListener interface are overridden.

import java.awt.Frame;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;

public class FrameClosing1 extends Frame implements WindowListener
{
  public FrameClosing1()
  {
    addWindowListener(this);
    setTitle("Frame closing Style 1");
    setSize(300, 300);
    setVisible(true);
  }
                      // override the 7 abstract methods of WindowListener
  public void windowClosing(WindowEvent e)
  {
    System.exit(0);
  }
  public void windowOpened(WindowEvent e)        {   }
  public void windowClosed(WindowEvent e)        {   }
  public void windowActivated(WindowEvent e)     {   }
  public void windowDeactivated(WindowEvent e)   {   }
  public void windowIconified(WindowEvent e)     {   }
  public void windowDeiconified(WindowEvent e)   {   }

  public static void main(String args[])
  {
    new FrameClosing1();
  }
}

The FrameClosing1 class extends Frame and implements java.awt.event.WindowListener interface. WindowListener is capable to close the window when Close icon on the title bar is clicked.

addWindowListener(this);

addWindowListener() method of Frame (inherited from Window) registers or links the frame with the WindowListener. The parameter "this" represents an object of WindowListener (or to say, an object of FrameClosing1 that implements WindowListener).

Out of the 7 methods of WindowListener, only one method windowClosing() is overridden with code and the other 6 are given empty implementations (empty body). All the seven methods takes a parameter of WindowEvent object.


What is WindowEvent?

We know whenever a component is interacted by the user, an event is generated. For example, when a button is clicked, the button raises ActionEvent. Similarly now, when the frame is clicked it raises WindowEvent and infact this object is passed as parameter to the seven methods. Handling the event raised by a component is the first and foremost step in event handling. By handling the WindowEvent, the application (here, frame) is closed with System.exit(0).

System.exit(0);

The above statement terminates the current application; in this case, the frame closes. The exit(0) indicates normal shutdown and any parameter other than 0 (like 1) is abnormal termination; of course, this is operating system dependent.