Why doesn't AWT Frame close? Ok. Lets close it!

Why doesn't AWT Frame close?


AWT Frame is written using the native code i.e. C/C++ and is not given the functionality to close by default on clicking the close button. There may be several reasons for this, like if we create an application using the Frame class, and some operation is going on and accidentally the user clicked the close button. Then what happens, the application closes and any saved data will be lost unless if we code what to happen when user closes the window. So, it is not provided by default, not only in the AWT but also in the Swing, but in Swing if you can just notice, you will observe the frame disposing but the application doesn't close. The application keeps running in the background. This is what the reason well, i thought of. Any other? Dropping a comment will be appreciated.

How can we close it?


Closing it is dead simple. All you need to know at first, is what action gets fired, when some operation on the Window is opened. It is obvious here, the WindowListener in the java.awt.event package (Ofcourse, all the AWT Event classes reside there). So it is clear i think so, that you will have to implement or write an anonymous inner class or do whatever you want to handle the operations on the AWT frame within your hands. So, in this example i will teach to just terminate (exit) the program when the close button on the Frame is clicked. Other code, ofcourse can be coded upon your need. Thanks, for reading and just look at the example.

The Example



import java.awt.*;
import java.awt.event.*;
class CloseAWTFrame extends Frame
{

Button b1;
public CloseAWTFrame()
{
setTitle("Hi! I am a Frame");
setSize(400,400);
setLayout(new FlowLayout());
setVisible(true);

// Just add a button (optional)
b1=new Button("I am a button!");
add(b1);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}

public static void main(String args[])
{
new CloseAWTFrame();
}
}


Explanation


WindowAdapter: An adapter class, simply it is an abstract class that implement the WindowListener interface, it's body is not provided and we have to. The use of this is, we need not implement all the abstract methods of the WindowListener interface, instead just a method or two depending upon our need can be implemented.

Why windowClosing(WindowEvent we)? The program has to exit at the time of closing the window i.e. when the user is clicking the button. For example, if you write windowClosed instead of WindowClosing then the window will not be closed because it means to terminate the program after the window is closed and the window will never be closed unless you write it in the windowClosing. 

System.exit(0): Nothing, close the program (i.e. terminate the current running JVM) normally. The 0 says it to terminate normally.

What still? Nothing, yet one more thing, if you just want to dispose the frame instead of terminating the entire program, it is your wish as said earlier, if you, then dispose() method is for you.