# G4P restore minimized window

**URL:** <https://discourse.processing.org/t/g4p-restore-minimized-window/45013>\
**Category:** Libraries\
**Created:** [August 31, 2024, 2:21pm UTC](https://discourse.processing.org/t/g4p-restore-minimized-window/45013 "2024-08-31T14:21:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcel](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Marcel](https://discourse.processing.org/u/Marcel)\
**Post date:** [August 31, 2024, 2:21pm UTC](https://discourse.processing.org/t/g4p-restore-minimized-window/45013/1 "2024-08-31T14:21:07Z")

</div>

Hi Quark (Peter),  
I would like to know how I can restore a minimized window (not the main GWindow). Tried the “setVisible” property to simulate minimizing and restoring but is seems that the window will unload after the visible property is set to “Hidden”. Is there a way to build a toggle button that restores or simulates the visibility of a window or restores the minimized window?  
Other quick question is that all my non main windows start as hidden, but at start up the windows are first quickly visible (drawn) before the are made “hidden”. Any thoughts on how to eliminate the flickering?  
Cheers Marcel.

---

<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 1, 2024, 10:12am UTC](https://discourse.processing.org/t/g4p-restore-minimized-window/45013/2 "2024-09-01T10:12:13Z")

</div>

The `setVisible` method is used to set the window’s visible flag which determines whether the OS shows the window, it does not _ **unload** _ anything 🤔

> [@Marcel](#):
>
> Is there a way to build a toggle button that restores or simulates the visibility of a window or restores the minimized window?

Provided the secondary GWindow uses the default JAVA2D renderer (not P2D or P3D) it is entirely possible to programatically minimize and restore secondary windows.

The following sketch creates a secondary window with a simple G4P label saying just that. The sketch window has 2 buttons to minimuze and restore the secondary window. I have create three utility functions to handle this so it can be used with multiple secondary windows.

```auto
/*
 Demonstrates fow to minimize and restore a G4P window using the
 default JAVA2D renderer.
 */
import g4p_controls.*;

GButton btnMinimize;
GButton btnRestore;
GWindowAWT win1;
GLabel label1;

public void setup() {
  size(320, 240, JAVA2D);
  createGUI();
}

public void draw() {
  background(230, 230, 255);
}

public javax.swing.JFrame getFrame(GWindowAWT window) {
  return (javax.swing.JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) window.getSurface().getNative()).getFrame();
}

public void minimizeWindow(GWindowAWT window) {
  javax.swing.JFrame frame = getFrame(window);
  frame.setExtendedState(java.awt.Frame.ICONIFIED);
}

public void restoreWindow(GWindowAWT window) {
  javax.swing.JFrame frame = getFrame(window);
  frame.setExtendedState(java.awt.Frame.NORMAL);
}

public void btnMinimize_click(GButton source, GEvent event) {
  minimizeWindow(win1);
}

public void btnRestore_click(GButton source, GEvent event) {
  restoreWindow(win1);
}

synchronized public void win1_draw(PApplet appc, GWinData data) {
  appc.background(255, 240, 160);
}

public void createGUI() {
  G4P.messagesEnabled(false);
  G4P.setDisplayFont("Arial", G4P.PLAIN, 16);
  surface.setTitle("Sketch Window");
  btnMinimize = new GButton(this, 20, 30, 280, 30);
  btnMinimize.setText("Minimize Window");
  btnMinimize.addEventHandler(this, "btnMinimize_click");
  btnRestore = new GButton(this, 20, 90, 280, 30);
  btnRestore.setText("Restore Window");
  btnRestore.addEventHandler(this, "btnRestore_click");
  win1 = (GWindowAWT)GWindow.getWindow(this, "Window title", 0, 0, 240, 120, JAVA2D);
  win1.setActionOnClose(G4P.KEEP_OPEN);
  win1.addDrawHandler(this, "win1_draw");
  label1 = new GLabel(win1, 20, 30, 200, 60);
  label1.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
  label1.setText("Secondary Window");
  label1.setLocalColorScheme(GCScheme.PURPLE_SCHEME);
  label1.setOpaque(true);
}

```

> [@Marcel](#):
>
> Any thoughts on how to eliminate the flickering?

I think someone has managed to do this but I can’t remember how 🙁

---

<div class="post-metadata">

**Author:** ![Marcel](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Marcel](https://discourse.processing.org/u/Marcel)\
**Post date:** [September 2, 2024, 10:30am UTC](https://discourse.processing.org/t/g4p-restore-minimized-window/45013/3 "2024-09-02T10:30:59Z")

</div>

Thanks again Peter for taking the time and making an effort to write the sample code.  
I’ve made minor changes so it works within the (auto) created code form the G4P GUI Tool.  
Cheers Marcel

---

<div class="post-metadata">

**Author:** ![Thundercat](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thundercat](https://discourse.processing.org/u/Thundercat)\
**Post date:** [September 7, 2024, 5:31pm UTC](https://discourse.processing.org/t/g4p-restore-minimized-window/45013/4 "2024-09-07T17:31:23Z")

</div>

When you first create the windows, set them to null.
