Home » Desktop Java » awt » Exit application when Frame is closed example

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Exit application when Frame is closed example

With this tutorial we shall show you how to exit from your application when a frame is closed. This is a very common use for most GUI applications.

This is very easy to do, all you have to do is:

  • Create a new WindowAdapter instance.
  • Override windowClosing method to customize the handling of that specific event. Now every time a window closes, this method will fire up.
  • Call System.exit(0) inside windowClosing method to exit the application when the window closes.

Let’s see the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class FrameCloseEventListener {

  public static void main(String[] args) {

// Create the frame

Frame frame = new Frame();

// Add a listener for the close event

frame.addWindowListener(new WindowAdapter() {

    public void windowClosing(WindowEvent evt) {

  // Exit the application




// Display the frame

int frameWidth = 300;

int frameHeight = 300;

frame.setSize(frameWidth, frameHeight);




This was an example on how to exit application when Frame is closed.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of