Java Frame Close WindowListener



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.

15 thoughts on “Java Frame Close WindowListener”

  1. 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. 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. 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. 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. 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. 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 Comment

Your email address will not be published.