# Question about handling "onClose" events for windows in G4P library

**URL:** <https://discourse.processing.org/t/question-about-handling-onclose-events-for-windows-in-g4p-library/20100>\
**Category:** Libraries\
**Created:** [April 24, 2020, 3:17pm UTC](https://discourse.processing.org/t/question-about-handling-onclose-events-for-windows-in-g4p-library/20100 "2020-04-24T15:17:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/david/32/8853_2.png) [@David](https://discourse.processing.org/u/David)\
**Post date:** [April 24, 2020, 3:17pm UTC](https://discourse.processing.org/t/question-about-handling-onclose-events-for-windows-in-g4p-library/20100/1 "2020-04-24T15:17:19Z")

</div>

I want to open a child window from the main window, but I also want to prevent multiple instances of the child window from being opened. I was able to do that by calling ‘setEnabled(false)’ on the button when the child window is opened. However, I also want the user to be able to re-open the child window if it has been closed. I thought I could do that by adding an ‘OnCloseHandler’ for the child window and calling ‘setEnabled(true)’ on the button, but it doesn’t appear that the handler is getting called (I added a ‘println()’ statement inside the handler, but nothing gets printed). What am I doing wrong?

Thanks.

```auto
import g4p_controls.*;

GButton button;
GWindow window;

public void setup() {
  size(400,200,JAVA2D);
  button = new GButton(this, 10, 40, 120, 20, "Open Child");
  button.addEventHandler(this,"buttonHandler");
}

public void draw() {
  background(179,237,179);
}

public void makeChildWindow () {
  window = GWindow.getWindow(this,"Child Window",400,100,400,200,JAVA2D);
  window.addDrawHandler(this,"windowDraw");
  window.addOnCloseHandler(this,"windowClose");
}

public void windowDraw(PApplet app, GWinData data) {
  app.background(179,237,179);
}

public void windowClose(GWindow window) {
  println("Closing child window...");
  button.setEnabled(true);
}

public void buttonHandler (GButton source, GEvent event) {
  if (event == GEvent.CLICKED) {
    println("Opening child window...");
    makeChildWindow();
    button.setEnabled(false);
  }
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 24, 2020, 6:30pm UTC](https://discourse.processing.org/t/question-about-handling-onclose-events-for-windows-in-g4p-library/20100/2 "2020-04-24T18:30:53Z")

</div>

You need to add an extra line to your` makeChildWindow()` method like this

```auto
public void makeChildWindow () {
  window = GWindow.getWindow(this, "Child Window", 400, 100, 400, 200, JAVA2D);
  window.setActionOnClose(G4P.CLOSE_WINDOW);
  window.addDrawHandler(this, "windowDraw");
  window.addOnCloseHandler(this, "windowClose");
}

```
