Java Closing AWT Window without Implementing WindowListener

Closing AWT Window without Implementing WindowListener:

This code is extension or continuation of this code adding button to AWT window. Why do this? Shorter code, no need implement unnecessary methods.

Register window listener to the frame and pass in WindowAdapter Object as shown below. Here we just implement windowClosing method but if we need others we could also add them inside WindowAdapter.

Code AWT GUI window Closing without WindowListener Implementation:

import java.awt.*;
import java.awt.event.*;

 * Created by asif on 8/13/2015.
public class SimpleAwtGui {

    // Declare a Frame type variable
    Frame frame;
    Button button;

    public SimpleAwtGui() {
        // Create Frame Object and pass in the Frame Name / title
        frame = new Frame("AWT GUI EXAMPLE");
        button = new Button("I am here Click Me");

    public static void main(String[] args) {

        // Create an instance of SimpleAwtGui
        SimpleAwtGui window = new SimpleAwtGui();

        // call the showFrame() function to display the window


    // Not necessary but good practice all codes inside this can be written inside main
    public void showFrame() {
        // set the size of the window
        frame.setSize(400, 400);

        // set the layout for the window
        frame.setLayout(new FlowLayout());

        // Add the button component to frame otherwise button won't show

        // Register window listener event to the frame without implementing WindowListener
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {

        // set the frame visible otherwise nothing will be shown

One thought on “Java Closing AWT Window without Implementing WindowListener

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s