How to close AWT Window in JavaWe 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() methodThere are many ways to override windowClosing() method:
Close AWT Window Example 1: Anonymous classIn the following example, we are implementing the windowClosing() method of Window using the Anonymous class WindowExample.java Output: Close AWT Window Example 2: extending WindowAdapterIn the following example, we are implementing the window closing functionality using the WindowAdapter class. AdapterExample.java Close AWT Window Example 3: implementing WindowListenerIn the following example, we are implementing the WindowListener interface to display the functionality of closing the AWT Window. WindowExample.java
Next TopicJava Swing Tutorial
|