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

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.

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);
  }
}
2 Likes

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

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");
}
3 Likes