# \[SOLVED\] G4P options in second window problem

**URL:** <https://discourse.processing.org/t/solved-g4p-options-in-second-window-problem/4552>\
**Category:** Libraries\
**Created:** [October 17, 2018, 9:26am UTC](https://discourse.processing.org/t/solved-g4p-options-in-second-window-problem/4552 "2018-10-17T09:26:51Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [October 17, 2018, 9:26am UTC](https://discourse.processing.org/t/solved-g4p-options-in-second-window-problem/4552/1 "2018-10-17T09:26:51Z")

</div>

when i use basic ( reduced from example ) code all works.

```auto
import g4p_controls.*;
GOption option0, option1, option2;

public void setup() {
  size(500,500);
  createGUI();
}

public void draw() {
  background(100, 100, 200);
}

public void createGUI(){
  int posX = 50, posY =20, dposY = 20;
  GToggleGroup tg = new GToggleGroup();
  
  option0 = new GOption(this, posX, posY, 80, 24, "A");
  option0.setTextAlign(null, GAlign.BOTTOM);

  option1 = new GOption(this, posX, posY+dposY, 80, 24, "B");
  option1.setTextAlign(null, GAlign.BOTTOM);
  
  option2 = new GOption(this, posX, posY+dposY+dposY, 80, 24, "C");
  option2.setTextAlign(null, GAlign.BOTTOM);
  
  // Group the option buttons
  tg.addControls(option0, option1, option2);
  // Must set one of the options true
  option1.setSelected(true);

}

// following is not needed for the option button group to work, 
// only to use the option mouse press/select events 
public void handleToggleControlEvents(GToggleControl source, GEvent event) { 
  if (source == option0) { println("option0"); } 
  else if (source == option1) { println("option1"); } 
  else if (source == option2) { println("option2"); }
}

```

but try that with a second window both windows react differently on a mouse click  
and the basic functionality  
on click on one option the other of that group deselects  
is lost.

```auto
import g4p_controls.*;
GOption option0, option1;
GOption woption0, woption1;

GWindow[] window;

public void setup() {
  size(100,100);
  createGUI();
  createWindows();
}

public void draw() {
  background(100, 100, 200);
}

public void createGUI(){
  int posX = 40, posY =20, dposY = 20;
  GToggleGroup tg = new GToggleGroup();
  
  option0 = new GOption(this, posX, posY+0*dposY, 80, 24, "A");
  option0.setTextAlign(null, GAlign.BOTTOM);

  option1 = new GOption(this, posX, posY+1*dposY, 80, 24, "B");
  option1.setTextAlign(null, GAlign.BOTTOM);
  option1.setSelected(true); // Must set one of the options true
  
  tg.addControls(option0, option1); // Group the option buttons
  
}

public void handleToggleControlEvents(GToggleControl source, GEvent event) { 
  if (source == option0) { println("option0"); } //option1.setSelected(false);
  else if (source == option1) { println("option1"); } //option0.setSelected(false);

  if (source == woption0) { println("woption0"); } //woption1.setSelected(false);
  else if (source == woption1) { println("woption1"); } //woption0.setSelected(false);

}

// _______________________________________________________________________
public void createWindows() {

  window = new GWindow[1];
  // for (int i = 0; i < 1; i++) {
  // window[i] = GWindow.getWindow(this, " ", 70+i*220, 160+i*50, 400, 400, JAVA2D);
  // }
  // menu window config
  window[0] = GWindow.getWindow(this, "window2", 70, 160, 100, 100, JAVA2D);
  window[0].setActionOnClose(G4P.CLOSE_WINDOW);
  //window[0].addOnCloseHandler(this, "windowClosing");
  //window[0].addData(new MyWinData());
  window[0].addDrawHandler(this, "windowDraw");
  //window[0].addMouseHandler(this, "windowMouse");
  //window[0].addKeyHandler(this, "windowKey");

  int posX = 40, posY =20, dposY = 20;
  GToggleGroup wtg = new GToggleGroup();
  
  woption0 = new GOption(window[0], posX, posY+0*dposY, 80, 24, "wA");
  woption0.setTextAlign(null, GAlign.BOTTOM);

  woption1 = new GOption(window[0], posX, posY+1*dposY, 80, 24, "wB");
  woption1.setTextAlign(null, GAlign.BOTTOM);
  woption1.setSelected(true); // Must set one of the options true
  
  wtg.addControls(option0, option1); // Group the option buttons
  
}

// _______________________________________________________________________
public void windowDraw(PApplet appc, GWinData data) {
 // MyWinData data2 = (MyWinData)data;
  appc.background(200,200,0);
}

```

what handler i would need to register?  
or just use a manual

```auto
optionX.setSelected(false);

```

handleToggleControlEvents does not take a this or window config.

---

<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:** [October 17, 2018, 10:44am UTC](https://discourse.processing.org/t/solved-g4p-options-in-second-window-problem/4552/2 "2018-10-17T10:44:40Z")

</div>

> [@kll](#):
>
> wtg.addControls(option0, option1); // Group the option buttons

You are adding the wrong option buttons should be

```auto
 wtg.addControls(woption0, woption1); // Group the option buttons

```

---

<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:** [October 17, 2018, 10:59am UTC](https://discourse.processing.org/t/solved-g4p-options-in-second-window-problem/4552/3 "2018-10-17T10:59:54Z")

</div>

Also in your second post you are setting an option true BEFORE adding it to the toggle group, Only use `setSelected` AFTER all the options have been added to the option group. (You didn’t do this in your first post).

In this code I have corrected the mistakes in your second post and shown how you can add separate event handlers for both sets of opuions.

```auto
import g4p_controls.*;
GOption option0, option1;
GOption woption0, woption1;

GWindow[] window;

public void setup() {
  size(200, 100);
  createGUI();
  createWindows();
}

public void draw() {
  background(100, 100, 200);
}

public void createGUI() {
  int posX = 40, posY =20, dposY = 20;
  GToggleGroup tg = new GToggleGroup();

  option0 = new GOption(this, posX, posY+0*dposY, 80, 24, "A");
  option0.setTextAlign(null, GAlign.BOTTOM);

  option1 = new GOption(this, posX, posY+1*dposY, 80, 24, "B");
  option1.setTextAlign(null, GAlign.BOTTOM);

  tg.addControls(option0, option1); // Group the option buttons
  option1.setSelected(true); // Must set one of the options true

  option0.addEventHandler(this, "option_clicked");
  option1.addEventHandler(this, "option_clicked");
}

public void option_clicked(GOption source, GEvent event) { 
  if (source == option0) { 
    println("option0");
  } //option1.setSelected(false);
  if (source == option1) { 
    println("option1");
  } //option0.setSelected(false);
} 

public void win_option_clicked(GOption source, GEvent event) { 
  if (source == woption0) { 
    println("woption0");
  } //option1.setSelected(false);
  if (source == woption1) { 
    println("woption1");
  } //option0.setSelected(false);
} 

// _______________________________________________________________________
public void createWindows() {

  window = new GWindow[1];
  // for (int i = 0; i < 1; i++) {
  // window[i] = GWindow.getWindow(this, " ", 70+i*220, 160+i*50, 400, 400, JAVA2D);
  // }
  // menu window config
  window[0] = GWindow.getWindow(this, "window2", 70, 160, 200, 100, JAVA2D);
  window[0].setActionOnClose(G4P.CLOSE_WINDOW);
  //window[0].addOnCloseHandler(this, "windowClosing");
  //window[0].addData(new MyWinData());
  window[0].addDrawHandler(this, "windowDraw");
  //window[0].addMouseHandler(this, "windowMouse");
  //window[0].addKeyHandler(this, "windowKey");

  int posX = 40, posY =20, dposY = 20;
  GToggleGroup wtg = new GToggleGroup();

  woption0 = new GOption(window[0], posX, posY+0*dposY, 80, 24, "wA");
  woption0.setTextAlign(null, GAlign.BOTTOM);

  woption1 = new GOption(window[0], posX, posY+1*dposY, 80, 24, "wB");
  woption1.setTextAlign(null, GAlign.BOTTOM);

  wtg.addControls(woption0, woption1); // Group the option buttons

  woption1.setSelected(true); // Must set one of the options true

  woption0.addEventHandler(this, "win_option_clicked");
  woption1.addEventHandler(this, "win_option_clicked");
}

// _______________________________________________________________________
synchronized public void windowDraw(PApplet appc, GWinData data) {
  // MyWinData data2 = (MyWinData)data;
  appc.background(200, 200, 0);
}

```

---

<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:** [October 17, 2018, 11:04am UTC](https://discourse.processing.org/t/solved-g4p-options-in-second-window-problem/4552/4 "2018-10-17T11:04:52Z")

</div>

thanks for the help and sorry for the typo when i created the “forum question version”

works well, but for understanding,  
why all 4 use

```auto
addEventHandler(this,

```

and not the window??

---

<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:** [October 17, 2018, 1:54pm UTC](https://discourse.processing.org/t/solved-g4p-options-in-second-window-problem/4552/5 "2018-10-17T13:54:26Z")

</div>

The `addEventHandler` method has two parameters to identify the _object_ that will handle the event and the name of the method in the objects class to use.

Even though the option buttons are in separate windows their events are handled in the _same object_ i.e. `this`

It is possible to define your class to handle events and this sketch demonstrates how to do that.

```auto
import g4p_controls.*;

GButton btn0, btn1;
HandleEvents he;

void setup() {
  size(220, 140);
  he = new HandleEvents();

  btn0 = new GButton(this, 20, 60, 80, 20, "Button 0");
  btn0.addEventHandler(this, "button_clicked");

  btn1 = new GButton(this, 120, 60, 80, 20, "Button 1");
  btn1.addEventHandler(he, "button_clicked");
}

void draw() {
  background(200, 200, 255);
}

public void button_clicked(GButton source, GEvent event) { 
  if (event == GEvent.CLICKED) { 
    println("Button clicked");
  }
} 

public class HandleEvents { // class must be public

  public void button_clicked(GButton source, GEvent event) { 
    if (event == GEvent.CLICKED) { 
      println("HandleEnevents object : button clicked");
    }
  }

}

```
