Window closing event handling

Submitted by:Andery Smith

Date added:06 November, 2013


Example shows how to handle window closing events. This is a very basic event handling when you are working on a UI Application.

Tags: handle window , closing event

Code Snippet:

package com.javacodegeeks.snippets.desktop;

import java.awt.Button;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class Main extends JFrame {

private static void showUI() {

Main jFrame = new Main();

jFrame.setSize(new Dimension(300, 250));

jFrame.add(new Button("Hello World"));

jFrame.addWindowListener(new WindowAdapter() {


public void windowClosing(WindowEvent e) {



public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {


public void run() {