# G4P get main window object

**URL:** <https://discourse.processing.org/t/g4p-get-main-window-object/12166>\
**Category:** Libraries\
**Created:** [June 18, 2019, 6:06pm UTC](https://discourse.processing.org/t/g4p-get-main-window-object/12166 "2019-06-18T18:06:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank-95](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@Frank-95](https://discourse.processing.org/u/Frank-95)\
**Post date:** [June 18, 2019, 6:06pm UTC](https://discourse.processing.org/t/g4p-get-main-window-object/12166/1 "2019-06-18T18:06:41Z")

</div>

Hello.

I’d like to get the main window GWindow object. My aim is to have the main window with some buttons, which when they are clicked set the main window as invisible and the new one as visible. When the new one is close, the main one should be set to visible again. Is it possible?  
If it’s not possible it’s also fine that main window objects are “disabled”, I don’t know which of the two way is easier.

Thank you

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 18, 2019, 7:28pm UTC](https://discourse.processing.org/t/g4p-get-main-window-object/12166/2 "2019-06-18T19:28:04Z")

</div>

> [@Frank-95](#):
>
> I’d like to get the main window GWindow object.

What do you mean by “main”? Do you have more than 1 GWindow object by chance?

Or do you mean the sketch’s main window? If it’s the latter, you can grab it via **getSurface()**:

- [PApplet](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#getSurface--)
- [PSurface](https://Processing.GitHub.io/processing-javadocs/core/processing/core/PSurface.html)

---

<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:** [June 19, 2019, 10:20am UTC](https://discourse.processing.org/t/g4p-get-main-window-object/12166/3 "2019-06-19T10:20:54Z")

</div>

If I understand what you want correctly then this sketch demonstrates how to do it.

```auto
import g4p_controls.*;

GButton btnOnMain, btnOnGwin1; 
GWindow gwin1;

public void setup() {
  size(320, 240, JAVA2D);
  createGUI();
  gwin1.setVisible(false);
}

public void draw() {
  background(200, 200, 255);
  fill(0);
  text("GWindow", 0, 0, width, 30);
}

// Draw method for second window
public void gwin1_draw(PApplet appc, GWinData data) {
  appc.background(255, 255, 200);
  appc.fill(0);
  appc.text("GWindow", 0, 0, appc.width, 30);
} 

// Button event handlers
public void btnOnMain_click(GButton source, GEvent event) { 
  getSurface().setVisible(false);
  gwin1.setVisible(true);
}

public void btnOnGwin1_click(GButton source, GEvent event) { 
  getSurface().setVisible(true);
  gwin1.setVisible(false);
} //_CODE_:btnOnGwin1:382918:

// 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("Main Sketch Window");
  btnOnMain = new GButton(this, 130, 200, 180, 30);
  btnOnMain.setText("Show Second Window");
  btnOnMain.addEventHandler(this, "btnOnMain_click");
  gwin1 = GWindow.getWindow(this, "GWindow 1", 0, 0, 240, 120, JAVA2D);
  gwin1.noLoop();
  gwin1.setActionOnClose(G4P.KEEP_OPEN);
  gwin1.addDrawHandler(this, "gwin1_draw");
  btnOnGwin1 = new GButton(gwin1, 10, 80, 220, 30);
  btnOnGwin1.setText("Show Main Window");
  btnOnGwin1.addEventHandler(this, "btnOnGwin1_click");
  gwin1.loop();
}

```

---

<div class="post-metadata">

**Author:** ![Frank-95](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@Frank-95](https://discourse.processing.org/u/Frank-95)\
**Post date:** [June 30, 2019, 4:59pm UTC](https://discourse.processing.org/t/g4p-get-main-window-object/12166/4 "2019-06-30T16:59:47Z")

</div>

Sorry for the belated answer, thank you very much, I got it. One more thing:

is it possible to show the main window again on window closure (with the X) rather than by using another button?

---

<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:** [June 30, 2019, 5:24pm UTC](https://discourse.processing.org/t/g4p-get-main-window-object/12166/5 "2019-06-30T17:24:09Z")

</div>

> [@Frank-95](#):
>
> is it possible to show the main window again on window closure (with the X) rather than by using another button?

Yes - this sketch does both 😁

```auto
import g4p_controls.*;

GButton btnOnMain, btnOnGwin1; 
GWindow gwin1;

public void setup() {
  size(320, 240, JAVA2D);
  createGUI();
  gwin1.setVisible(false);
}

public void draw() {
  background(200, 200, 255);
  fill(0);
  text("GWindow", 0, 0, width, 30);
}

// Draw method for second window
public void gwin1_draw(PApplet appc, GWinData data) {
  appc.background(255, 255, 200);
  appc.fill(0);
  appc.text("GWindow", 0, 0, appc.width, 30);
} 

// Button event handlers
public void btnOnMain_click(GButton source, GEvent event) { 
  getSurface().setVisible(false);
  gwin1.setVisible(true);
}

public void btnOnGwin1_click(GButton source, GEvent event) { 
  getSurface().setVisible(true);
  gwin1.setVisible(false);
} //_CODE_:btnOnGwin1:382918:

public void closewindow(GWindow window) { //_CODE_:gwin1:604367:
  getSurface().setVisible(true);
} //_CODE_:gwin1:604367:

// 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("Main Sketch Window");
  btnOnMain = new GButton(this, 130, 200, 180, 30);
  btnOnMain.setText("Show Second Window");
  btnOnMain.addEventHandler(this, "btnOnMain_click");
  gwin1 = GWindow.getWindow(this, "GWindow 1", 0, 0, 240, 120, JAVA2D);
  gwin1.noLoop();
  gwin1.setActionOnClose(G4P.CLOSE_WINDOW); // Must allow window to close :-)
  gwin1.addDrawHandler(this, "gwin1_draw");
  gwin1.addOnCloseHandler(this, "closewindow");
  btnOnGwin1 = new GButton(gwin1, 10, 80, 220, 30);
  btnOnGwin1.setText("Show Main Window");
  btnOnGwin1.addEventHandler(this, "btnOnGwin1_click");
  gwin1.loop();
}

```

---

<div class="post-metadata">

**Author:** ![Frank-95](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@Frank-95](https://discourse.processing.org/u/Frank-95)\
**Post date:** [June 30, 2019, 5:25pm UTC](https://discourse.processing.org/t/g4p-get-main-window-object/12166/6 "2019-06-30T17:25:45Z")

</div>

Thank you very much!
