Close AWT Window

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

How to close AWT Window in Java

We can close the AWT Window or Frame by calling dispose () or System.exit() inside windowClosing() method. The windowClosing() method is found in WindowListener interface and WindowAdapter class.

The WindowAdapter class implements WindowListener interfaces. It provides the default implementation of all the 7 methods of WindowListener interface. To override the windowClosing() method, you can either use WindowAdapter class or WindowListener interface.

If you implement the WindowListener interface, you will be forced to override all the 7 methods of WindowListener interface. So it is better to use WindowAdapter class.

Different ways to override windowClosing() method

There are many ways to override windowClosing() method:

  • By anonymous class
  • By inheriting WindowAdapter class
  • By implementing WindowListener interface

Close AWT Window Example 1: Anonymous class

  1. import java.awt.*;  
  2. import java.awt.event.WindowEvent;  
  3. import java.awt.event.WindowListener;  
  4. public class WindowExample extends Frame{  
  5.     WindowExample(){  
  6.         addWindowListener(new WindowAdapter(){  
  7.             public void windowClosing(WindowEvent e) {  
  8.                 dispose();  
  9.             }  
  10.         });  
  11.         setSize(400,400);  
  12.         setLayout(null);  
  13.         setVisible(true);  
  14.     }  
  15. public static void main(String[] args) {  
  16.     new WindowExample();  
  17. }  

Close AWT Window Example 2: extending WindowAdapter

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class AdapterExample extends WindowAdapter{  
  4.     Frame f;  
  5.     AdapterExample(){  
  6.         f=new Frame();  
  7.         f.addWindowListener(this);  
  8.           
  9.         f.setSize(400,400);  
  10.         f.setLayout(null);  
  11.         f.setVisible(true);  
  12.     }  
  13. public void windowClosing(WindowEvent e) {  
  14.     f.dispose();  
  15. }  
  16. public static void main(String[] args) {  
  17.     new AdapterExample();  
  18. }  
  19. }  

Close AWT Window Example 3: implementing WindowListener

  1. import java.awt.*;  
  2. import java.awt.event.WindowEvent;  
  3. import java.awt.event.WindowListener;  
  4. public class WindowExample extends Frame implements WindowListener{  
  5.     WindowExample(){  
  6.         addWindowListener(this);  
  7.           
  8.         setSize(400,400);  
  9.         setLayout(null);  
  10.         setVisible(true);  
  11.     }  
  12.       
  13. public static void main(String[] args) {  
  14.     new WindowExample();  
  15. }  
  16. public void windowActivated(WindowEvent e) {}  
  17. public void windowClosed(WindowEvent e) {}  
  18. public void windowClosing(WindowEvent e) {  
  19.     dispose();  
  20. }  
  21. public void windowDeactivated(WindowEvent e) {}  
  22. public void windowDeiconified(WindowEvent e) {}  
  23. public void windowIconified(WindowEvent e) {}  
  24. public void windowOpened(WindowEvent arg0) {}  
  25. }