G4P trouble trying to hide a window when the operating system close button is pressed

First off, thank you @quark for the great libraries and gui building tools. They have been really helpful recently!

I am working on a project that involves several windows, all of which I need to be reusable while the program is running. On a few of the windows which involve user input I have added a button that can be clicked to “close” (hide) the window. One of the windows that I am using is meant to display an image and just an image, but needs to be able to be hidden and reused in the future.

Below is a simplified code snippet that I generated with the G4P builder tool.

synchronized public void win_draw1(PApplet appc, GWinData data) { //_CODE_:window1:920231:
} //_CODE_:window1:920231:

public void window1OnClose(GWindow window) { //_CODE_:window1:910966:
  println("window1 - window closed at " + millis());
} //_CODE_:window1:910966:

// Create all the GUI controls. 
// autogenerated do not edit
public void createGUI(){
  surface.setTitle("Sketch Window");
  window1 = GWindow.getWindow(this, "Window title", 0, 0, 240, 120, JAVA2D);
  window1.addDrawHandler(this, "win_draw1");
  window1.addOnCloseHandler(this, "window1OnClose");

// Variable declarations 
// autogenerated do not edit
GWindow window1;

I would like to be able to hide the window by pressing the built-in exit/close button, but haven’t found a way to do that yet. If it’s not possible, then I will probably resort to adding a button on the window I want to hide.

Am I missing something obvious here about how G4P windows work? Is there a way to detect if the operating system exit button has been clicked?

Thank you,


1 Like

I forgot to mention that while trying the above technique for hiding the window when the close button is pressed I found out that the onClose event handler is not even being called when I have the the window set with KEEP_OPEN. If I have the window set with CLOSE_WINDOW then the window hides correctly, but does not work when it is unhidden again.

it closes, not hides!

on decorated windows [x] can

  • keep open ( ignore )
  • close
  • close all

you would have the option HIDE instead CLOSE?
even if that would be added, have to wait next revision,
but i think you have the show hide toggle buttons (? in MAIN? ) already,
so KEEP OPEN seems best way for now,
but could undecorate the utility windows???

Hmm, that makes sense. I tried to make the window visible after closing and it did show an empty window, but it clearly wasn’t working. I will find a workaround to the problem for now. Thank you for your help!

For anyone whos interested:

I ended up putting the gui initilaizer method inside the on close method. This way, when the window is closes it will reinit as a hidden window that can then be used again. This isn’t ideal, but it works for what I’m doing.


so how did you solve the problem? im having the same issue like yours. Actually i want to reuse my textarea after it is close but when i reopen the 2nd window the textarea is gone forgood. so how did you figured the close button? may i see the code? pls that would be helpful to me :pray:

Not sure how I missed this discussion when it first came round but never mind I have seen it now. :smile:

G4P does not support hiding (making the window invisible) when the ‘close’ button is clicked but I will investigate the possibility of introducing it when I get a chance.

If you had created a G4P window like this -
myWindow = GWindow.getWindow(this, 40, 20, 480, 320, JAVA2D);

then close the window, it still exists in memory because myWindow holds a reference to the window preventing its resources being garbage collected by the JVM. This next bit is important!

If you select the CLOSE_WINDOW option and click on the ‘close’ button G4P disposes of all the G4P controls on that window releasing their resources, but as I said previously, myWindow still references the window now empty and useless. To release the window resources we need to set the reference to null like this -
myWindow = null;

More information about using G4P windows can be found here.