Initializing values in the ControlP5 button library

In the below code snippet I find the “.setValue(0)” line triggers the “BrowseOpenOutputFile()” event handler.This caused an undesired file browser dialog (called in the event handler code) to appear at sketch startup. Is there a way to set the value without triggering the event during initialization?

jControl = new ControlP5(this);
    // .setValue(0)  // Not called because it triggers the event handler
    .setLabel("Open Output File")
    .setSize( 100, 20 )
    .setColor( DataToggleColors )
1 Like

Were you able to resolve this issue?

I’m not certain that I understand what you are trying to do. Are you trying to call setValue(0) on a ControlP5.Button? Why? This just passes 0 to your callback function. If you don’t want to do this, don’t call Button.setValue().

For examples of how this is used, see the Examples in the ControlP5 Button documentation.

Call .setValue(0) before you register the callback.

@micycle you mean, call setValue() directly on the ControlP5 instance? Why?

I mean calling it on the button (which is what his code is doing now).

Here is a quick working example. A key issue here is that both cp5.addButton("theFunction") and new Button(cp5, "theFunction") seem to have the same result – so one solution is to pass “” and then add a callBack after. If someone knows a more elegant way, please share.

 * CP5ButtonThenCallBack
 * CP5 set button value THEN register callback
 * 2020-03-05 Jeremy Douglass -- Processing 3.4
 * Do this to avoid passing a value on init that triggers the callback.
 * Discussion:
import controlP5.*;

ControlP5 cp5;

void setup() {
  size(200, 200);
  cp5 = new ControlP5(this);
  // 1. create the button with no callback
  Button aButton = new Button(cp5, ""); // (cp5, "mybutton")
  // 2. configure the button
    .setLabel("Open Output File")
    .setSize( 100, 20 )
    .setPosition(10, 10)
  // 3. register the callback -- see example from:
    new CallbackListener() {
    public void controlEvent(CallbackEvent event) {
      if (event.getAction() == ControlP5.ACTION_RELEASED) {

void draw(){}

void mybutton () { 
1 Like