Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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
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
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.