G4P get main window object

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

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():

1 Like

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

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();
}
1 Like

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?

Yes - this sketch does both :grin:

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();
}
1 Like

Thank you very much!