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
In the following example, we are implementing the windowClosing() method of Window using the Anonymous class
WindowExample.java
// importing necessary awt libraries
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
// class which inherits the Frame class
public class WindowExample extends Frame {
// class constructor
WindowExample() {
// adding WindowListener to the Frame
// and using the windowClosing() method of WindowAdapter class
addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
dispose();
}
});
// setting the size, layout and visibility of frame
setSize (400, 400);
setLayout (null);
setVisible (true);
}
// main method
public static void main (String[] args) {
new WindowExample();
}
Output:

Close AWT Window Example 2: extending WindowAdapter
In the following example, we are implementing the window closing functionality using the WindowAdapter class.
AdapterExample.java
// importing the necessary libraries
import java.awt.*;
import java.awt.event.*;
// class which inherits the WindowAdapter class
public class AdapterExample extends WindowAdapter {
// object of Frame
Frame f;
// class constructor
AdapterExample() {
// creating the frame
f = new Frame();
// adding WindowListener to the frame
f.addWindowListener (this);
// setting the size, layout and visibility of frame
f.setSize (400, 400);
f.setLayout (null);
f.setVisible (true);
}
// overriding the windowClosing() method
public void windowClosing (WindowEvent e) {
f.dispose();
}
// main method
public static void main(String[] args) {
new AdapterExample();
}
}
Close AWT Window Example 3: implementing WindowListener
In the following example, we are implementing the WindowListener interface to display the functionality of closing the AWT Window.
WindowExample.java
// importing the necessary libraries
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
// class which inherits Frame class and implement the WindowListener interface
public class WindowExample extends Frame implements WindowListener {
// constructor
WindowExample() {
// adding WindowListener to frame
addWindowListener(this);
// setting the size, layout and visibility of frame
setSize(400,400);
setLayout(null);
setVisible(true);
}
// main method
public static void main(String[] args) {
new WindowExample();
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
dispose();
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent arg0) {}
}
If you like this answer, you can give me a coffee by click here (view Ads)
Leave a Review