# Run function on 'Close'

**URL:** <https://discourse.processing.org/t/run-function-on-close/1228>\
**Category:** Coding Questions\
**Created:** [June 25, 2018, 12:33am UTC](https://discourse.processing.org/t/run-function-on-close/1228 "2018-06-25T00:33:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![madscientist](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@madscientist](https://discourse.processing.org/u/madscientist)\
**Post date:** [June 25, 2018, 12:33am UTC](https://discourse.processing.org/t/run-function-on-close/1228/1 "2018-06-25T00:33:46Z")

</div>

OK, so I’m trying to create an app where the user is prompted to save when the red ‘X’ a.k.a. the ‘Close’ button is clicked, but I can’t figure out how to run a function when that is clicked. I DO know how to prevent the program from automatically exiting when the ‘X’ is clicked, see below.

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/17310/intercept-window-closure-in-p2d-renderer-processing-3)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

  
I’m using the JAVA2D renderer because I’m using the G4P library for my GUI. I’ve already got my save function written, so now I just have to call it in some sort of handler maybe?

---

<div class="post-metadata">

**Author:** ![madscientist](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@madscientist](https://discourse.processing.org/u/madscientist)\
**Post date:** [June 25, 2018, 1:39am UTC](https://discourse.processing.org/t/run-function-on-close/1228/2 "2018-06-25T01:39:55Z")

</div>

I figured it out. 😀 I just create a WindowListener Object.  
[https://docs.oracle.com/javase/9/docs/api/java/awt/event/WindowListener.html](https://docs.oracle.com/javase/9/docs/api/java/awt/event/WindowListener.html)
