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

**URL:** <https://discourse.processing.org/t/g4p-trouble-trying-to-hide-a-window-when-the-operating-system-close-button-is-pressed/9209>\
**Category:** Libraries\
**Created:** [March 11, 2019, 11:41pm UTC](https://discourse.processing.org/t/g4p-trouble-trying-to-hide-a-window-when-the-operating-system-close-button-is-pressed/9209 "2019-03-11T23:41:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)\
**Post date:** [March 11, 2019, 11:41pm UTC](https://discourse.processing.org/t/g4p-trouble-trying-to-hide-a-window-when-the-operating-system-close-button-is-pressed/9209/1 "2019-03-11T23:41:33Z")

</div>

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.

```auto
synchronized public void win_draw1(PApplet appc, GWinData data) { //_CODE_:window1:920231:
  appc.background(230);
} //_CODE_:window1:920231:

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

// Create all the GUI controls. 
// autogenerated do not edit
public void createGUI(){
  G4P.messagesEnabled(false);
  G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
  G4P.setMouseOverEnabled(false);
  surface.setTitle("Sketch Window");
  window1 = GWindow.getWindow(this, "Window title", 0, 0, 240, 120, JAVA2D);
  window1.noLoop();
  window1.setActionOnClose(G4P.KEEP_OPEN);
  window1.addDrawHandler(this, "win_draw1");
  window1.addOnCloseHandler(this, "window1OnClose");
  window1.loop();
}

// 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,

Grayson

---

<div class="post-metadata">

**Author:** ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)\
**Post date:** [March 11, 2019, 11:45pm UTC](https://discourse.processing.org/t/g4p-trouble-trying-to-hide-a-window-when-the-operating-system-close-button-is-pressed/9209/2 "2019-03-11T23:45:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 12, 2019, 2:31am UTC](https://discourse.processing.org/t/g4p-trouble-trying-to-hide-a-window-when-the-operating-system-close-button-is-pressed/9209/3 "2019-03-12T02:31:16Z")

</div>

> [@graysonh](#):
>
> CLOSE\_WINDOW then the window hides correctly, but does not work when it is unhidden again.

it closes, not hides!

on decorated windows can

- keep open ( ignore )
- close
- close all

only  
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???

---

<div class="post-metadata">

**Author:** ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)\
**Post date:** [March 12, 2019, 12:17pm UTC](https://discourse.processing.org/t/g4p-trouble-trying-to-hide-a-window-when-the-operating-system-close-button-is-pressed/9209/4 "2019-03-12T12:17:38Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)\
**Post date:** [March 12, 2019, 12:36pm UTC](https://discourse.processing.org/t/g4p-trouble-trying-to-hide-a-window-when-the-operating-system-close-button-is-pressed/9209/5 "2019-03-12T12:36:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RNWA](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@RNWA](https://discourse.processing.org/u/RNWA)\
**Post date:** [September 10, 2022, 2:04am UTC](https://discourse.processing.org/t/g4p-trouble-trying-to-hide-a-window-when-the-operating-system-close-button-is-pressed/9209/6 "2022-09-10T02:04:00Z")

</div>

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 🙏

---

<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:** [September 10, 2022, 8:57am UTC](https://discourse.processing.org/t/g4p-trouble-trying-to-hide-a-window-when-the-operating-system-close-button-is-pressed/9209/7 "2022-09-10T08:57:27Z")

</div>

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

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](http://www.lagers.org.uk/g4p/guides/g03-windows.html).
