[SOLVED] G4P options in second window problem

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

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.

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

optionX.setSelected(false);

handleToggleControlEvents does not take a this or window config.

1 Like

You are adding the wrong option buttons should be

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

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.

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);
}
3 Likes

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

addEventHandler(this,

and not the window??

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.

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");
    }
  }

}
2 Likes