We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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");
}
Answers
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.
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.
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... :)