G4P setSelected(true) on checkbox does not trigger the event handler function

edited December 2014 in Library Questions

My expected behavior is that instructing a checkbox to be selected or unselected would not only visually check the box, but also run the associated event handler function. This is the behavior of sliders, that the event handler gets called upon setValue-- so I'm curious if I should expect this behavior from checkboxes or was there a good reason for checkboxes not to run the event handler when the state is changed via setSelected?

Below is code that demonstrates this. By moving the mouse from side to the other, I'm attempting to trigger the event handler by using the setSelected function. The checkbox visually changes state, but the event handler is never called.

Thank you. I love this library and find it amazingly helpful, but suspected this might be a bug and cause redundant coding to circumvent.


import g4p_controls.*;
GCheckbox checkbox1; 

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

public void draw(){
  if(mouseX > width/2){
   checkbox1.setSelected(true); //this should run the function checkbox1_clicked1?
  }
  else{
   checkbox1.setSelected(false); //this should run the function checkbox1_clicked1? 
  }
}

public void checkbox1_clicked1(GCheckbox source, GEvent event) { 
  println("triggered");
} 



public void createGUI(){
  G4P.messagesEnabled(false);
  G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
  G4P.setCursor(ARROW);
  if(frame != null)
    frame.setTitle("Sketch Window");
  checkbox1 = new GCheckbox(this, 119, 133, 120, 20);
  checkbox1.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
  checkbox1.setText("checkbox text");
  checkbox1.setOpaque(false);
  checkbox1.addEventHandler(this, "checkbox1_clicked1");
}
Tagged:

Answers

  • edited December 2014 Answer ✓

    My expected behavior is that instructing a checkbox to be selected or unselected would not only visually check the box, but also run the associated event handler function.

    All toggle controls behave like this by design.

    When the G4P user creates code to set/clear a checkbox they may not want to generate an event. If they want to then they can add code to do that e.g.

    public void draw(){
      if(mouseX > width/2){
        checkbox1.setSelected(true);
        checkbox1_clicked1(checkbox1, GEvent.SELECTED);
      }
      else{
        checkbox1.setSelected(false);
        checkbox1_clicked1(checkbox1, GEvent.DESELECTED);
      }
    }
    

    This is the behavior of sliders, that the event handler gets called upon setValue

    The reason that GSlider, GSlider2D and GKnob controls generate events is because the can apply easing. Which means that when you use setValue() the slider thumb moves gradually (eases) to that value rather than changing abruptly. getValue always returns the value indicated by the thumb position so the control generates events during the easing so the control is always in sync with the action.

    The following example demonstrates easing.

    // Need G4P library
    import g4p_controls.*;
    
    public void setup() {
      size(480, 150, JAVA2D);
      createGUI();
      customGUI();
    }
    
    public void draw() {
      background(230);
    }
    
    void mouseMoved() {
      float fx = (1.0 * mouseX) / width;
      float fy = (1.0 * mouseY) / height;
    
      s1.setValue(fx);
      k1.setValue(fx);
      s2d1.setValueXY(fx, fy);
    }
    
    public void customGUI() {
      s2d1.setEasing(20.0);
    }
    
    public void slider1_change1(GSlider source, GEvent event) {
      println("GSlider >> GEvent." + event + " @ " + millis());
    }
    
    public void knob1_turn1(GKnob source, GEvent event) {
      println("kGKnob >> GEvent." + event + " @ " + millis());
    }
    
    public void slider2d1_change1(GSlider2D source, GEvent event) {
      println("GSlider2D >> GEvent." + event + " @ " + millis());
    }
    
    // Create all the GUI controls. 
    // autogenerated do not edit
    public void createGUI() {
      G4P.messagesEnabled(false);
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
      G4P.setCursor(ARROW);
      if (frame != null)
        frame.setTitle("Sketch Window");
      s1 = new GSlider(this, 22, 30, 100, 40, 10.0);
      s1.setLimits(0.5, 0.0, 1.0);
      s1.setEasing(20.0);
      s1.setNumberFormat(G4P.DECIMAL, 2);
      s1.setOpaque(false);
      s1.addEventHandler(this, "slider1_change1");
      k1 = new GKnob(this, 170, 27, 60, 60, 0.8);
      k1.setTurnRange(110, 70);
      k1.setTurnMode(GKnob.CTRL_HORIZONTAL);
      k1.setSensitivity(1);
      k1.setShowArcOnly(false);
      k1.setOverArcOnly(false);
      k1.setIncludeOverBezel(false);
      k1.setShowTrack(true);
      k1.setLimits(0.5, 0.0, 1.0);
      k1.setShowTicks(true);
      k1.setEasing(20.0);
      k1.setOpaque(false);
      k1.addEventHandler(this, "knob1_turn1");
      s2d1 = new GSlider2D(this, 277, 31, 155, 75);
      s2d1.setLimitsX(0.5, 0.0, 1.0);
      s2d1.setLimitsY(0.5, 0.0, 1.0);
      s2d1.setNumberFormat(G4P.DECIMAL, 2);
      s2d1.setOpaque(true);
      s2d1.addEventHandler(this, "slider2d1_change1");
    }
    
    GSlider s1; 
    GKnob k1; 
    GSlider2D s2d1; 
    

    BTW glad you like G4P :)

  • Ahh, that makes sense. Thank you for explaining and also for including that helpful example of how to call the event handler! Your prompt thoroughness saved me several hours of frustration, I think... :)

Sign In or Register to comment.