Java Frame Close WindowListener

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.

Java Frame Close WindowListener

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.

15 thoughts on “Java Frame Close WindowListener

  1. shuvodip pabi

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class MouseClick extends JFrame implements ActionListener{
    JButton b1,b2;
    JLabel lbl;
    int clicked;
    final String str=(“Number Of Times OK-Button Clicked”);
    public MouseClick(String strName){
    super(strName);
    getContentPane().setLayout(new FlowLayout());
    JPanel p=new JPanel();
    b1=new JButton(“OK”);
    b2=new JButton(“QUIT”);
    p.setLayout(new FlowLayout());
    p.add(b1);
    p.add(b2);
    lbl=new JLabel(str+clicked,JLabel.CENTER);
    getContentPane().add(p);
    getContentPane().add(lbl);
    b1.addActionListener(this);
    b2.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
    String s=e.getActionCommand();
    if(“OK”.equals(s)){
    clicked++;
    lbl.setText(str + clicked);
    }
    if(“QUIT”.equals(s)){
    System.exit(0);
    }
    }
    public static void main(String[]ar){
    MouseClick mc=new MouseClick(“MOUSE CLICK COUNT”);
    mc.setSize(400,400);
    mc.setVisible(true);

    }
    }
    // How I add mouse ENTER & OUT facility in this program.

  2. Amit Mahle

    import java.awt.*;
    import java.awt.event.*;

    class MyFrame extends Frame
    {

    public static void main(String args[])
    {
    MyFrame f=new MyFrame();
    f.setTitle(“My AWT Frame”);
    f.setSize(300,400);
    f.setVisible(true);
    f.addWindowListener(new Myclass());
    }
    }
    class Myclass implements WindowListener
    {
    // override the 7 abstract methods of WindowListener
    public void windowActivated(WindowEvent e){}
    public void windowDeactivated(WindowEvent e){}
    public void windowIconified(WindowEvent e){}
    public void windowDeconified(WindowEvent e){}
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    public void windowClosed(WindowEvent e){}
    public void windowOpened(WindowEvent e){}
    }

    // this program is not run
    there is an error Myclass is not abstract and we can not override the abstract method of WL interface

      1. Amit Mahle

        thanks sir,
        please tell me we can put the access specifer before constructor in java?
        from my knowledge of c++ we r not use access specifer ……

  3. vicky

    import java.awt.*;
    import java.awt.event.*;
    public class Demo extends Frame
    {

    public Demo(String title)
    {

    super(title) ;
    Label lab=new Label();
    lab.setText(“ok”);
    add(lab,”North”);

    }

    public static void main(String args[])
    {

    Demo d=new Demo(“pl”);
    d.setVisible(true);
    d.setSize(400,400);

    d.addWindowListener(new WindowAdapter()
    {
    public void WindowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });

    }

    }

    /* while executing this prog. it is not colosing the window after all i hv used window listener plg explain */

    1. S. Nageswara Rao, Corporate Trainer Post author

      Try this:

      import java.awt.*;
      import java.awt.event.*;
      public class Demo extends Frame
      {
      public Demo()
      {
      Label lab=new Label();
      lab.setText(“ok”);
      add(lab,”North”);
      }
      public static void main(String args[])
      {
      Demo d=new Demo();
      d.setVisible(true);
      d.setSize(400,400);
      d.addWindowListener(new WindowAdapter()
      {
      public void windowClosing(WindowEvent e)
      {
      System.exit(0);
      }
      });

      }
      }

    2. upendra

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.JFrame;
      public class Demo1 extends JFrame
      {

      public Demo1(String title)
      {

      super(title);
      Label lab=new Label();
      lab.setText(“ok”);
      add(lab,”North”);

      }

      public static void main(String args[])
      {

      Demo1 d=new Demo1(“pl”);
      d.setVisible(true);
      d.setSize(400,400);
      d.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      d.addWindowListener(new WindowAdapter()
      {
      public void WindowClosing(WindowEvent e)
      {
      System.exit(0);
      }
      });

      }

      }

Leave a Reply

Your email address will not be published. Required fields are marked *