Table of contents
1.
Introduction
2.
Close Window AWT
3.
Ways to Override windowClosing() method
4.
Examples
4.1.
Close AWT window Example 1
4.2.
Close AWT window Example 2
4.2.1.
Close AWT window Example 3
5.
Frequently Asked Questions
5.1.
How can we close AWT window?
5.2.
How do you close a windows GUI?
5.3.
How to close a Window in Java?
5.4.
What are the different ways to override the windowClosing() method?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Close AWT Window

Author yuvatimankar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

AWT window is an API to create GUI(Graphical user interface) or window-based application in java. Components of AWT are platform-independent which means components are represented based on the view of the operating system. 

In this article, we are going to discuss how to close AWT Window with the help of the Java program. We will see some examples in which we have closed AWT window. So stay till the end.

Also Read About, Multithreading in java and Hashcode Method in Java

Close Window AWT

A window AWT is nothing but an object with a top-level window that has no borders and menubar.We can close AWT Window in java by calling System.exit() or dispose() within windowClosing() method. The windowClosing() function is discovered in WindowAdapter class and WindowListener interface. 

A WindowListener interface is carried out by WindowAdapter class. For overriding the windowClosing() function, We can use the WindowListener interface or WindowAdapter class. If we execute the WindowListener interface, we are forced to override all the methods of the WindowListener Interface. This is the reason why we should use WindowAdapter class.

Read More About, Basics of Java

Ways to Override windowClosing() method

There are different ways to override the windowClosing() method. Some of the methods are as follows:

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

Examples

Let us see some examples for Close AWT window,

Close AWT window Example 1

In this example, we will implement the windowClosing() method by inheriting WindowAdapter class:

import java.awt.*;    
import java.awt.event.*;    
  
 
public class AdapterExample extends WindowAdapter {    
 
    Frame f;    
// class constructor  
    AdapterExample() {    
 
        f = new Frame();    


        f.addWindowListener (this);    
        
// setting the size, layout and visibility  
        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();    
}    
}    
You can also try this code with Online Java Compiler
Run Code

 

Output:

Close AWT Window Output

 

In the above code, we have implemented the windowClosing() method by inheriting the windowAdaptor class, as we can see from the above code, we have inherited the windowAdaptor class to AdaptorExample() and then we have overridden the windowClosing() method and in the main function, we have called AdaptorExample class to close AWT window.

Close AWT window Example 2

In this example, we will implement the windowClosing() method by implementing WindowListener interface:

import java.awt.*;    
import java.awt.event.WindowEvent;    
import java.awt.event.WindowListener;    


public class WindowExample extends Frame implements WindowListener {    
 
    WindowExample() {    
 
        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) {}    
}    
You can also try this code with Online Java Compiler
Run Code

 

Output:

Close AWT Window Output

 

You can practice by yourself with the help of online java compiler.

In the above example, as we can see we have implemented the program for closing the AWT window. In this program, we have implemented the windowClosing() method by inheriting the windowListener interface and then we have used some methods to close AWT window.

Close AWT window Example 3

In this example, we will implement the windowClosing() method by anonymous class:

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();    
}    
You can also try this code with Online Java Compiler
Run Code

 

This code will also generate the same output as above, as we are seeing different ways to close the AWT window.

You can also read about the Multiple Inheritance in Java.

Frequently Asked Questions

How can we close AWT window?

AWT window can be closed with the help of methods such as System.exit() or dispute() within the windowClosing() method.

How do you close a windows GUI?

We can close a windows GUI by using System.exit(0). By using this method we can close the entire process running.

How to close a Window in Java?

We can close A window in Java by using javax.swing.Jframe or by using java.awt.Frame.

What are the different ways to override the windowClosing() method?

The different ways to override windowClosing() methods are: By anonymous class, by inheriting WindowAdaptor class, and by implementing the WindowListener interface.

Conclusion

This article extensively discussed the close AWT window and different ways to override the windowClosing() method. We started with an introduction to the AWT window, then saw examples to close AWT window. 

If you want to learn more about such topics, you can see Introduction to JavaBasics of Java, Why is Java Platform Independent Learn getting started with Java, and Learn OOPs in Java, also, you can enroll in the course Basics of Java with Data structure and algorithms. Also, you can visit our website, code studio!

You can also refer to our guided path on Coding Ninjas Studio to upskill yourself in Data structure and algorithmsCompetitive Programming, Javascriptand System Design. Do upvote our blogs if you find them helpful and engaging!

Happy learning!

Live masterclass