# Initializing values in the ControlP5 button library

**URL:** https://discourse.processing.org/t/initializing-values-in-the-controlp5-button-library/17578
**Category:** Libraries
**Created:** [February 6, 2020, 4:52am UTC](https://discourse.processing.org/t/initializing-values-in-the-controlp5-button-library/17578 "2020-02-06T04:52:11Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![spflanze](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@spflanze](https://discourse.processing.org/u/spflanze)
#### Post date: [February 6, 2020, 4:52am UTC](https://discourse.processing.org/t/initializing-values-in-the-controlp5-button-library/17578/1 "2020-02-06T04:52:11Z")

</div>

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?

```auto
jControl = new ControlP5(this);
		
jControl.addButton("BrowseOpenOutputFile")
    // .setValue(0) // Not called because it triggers the event handler
    .setLabel("Open Output File")
    .setSize( 100, 20 )
    .setPosition(200,0)
    .setColor( DataToggleColors )
    .updateSize()
;

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [February 19, 2020, 5:56am UTC](https://discourse.processing.org/t/initializing-values-in-the-controlp5-button-library/17578/2 "2020-02-19T05:56:54Z")

</div>

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.

[http://www.sojamo.com/libraries/controlP5/reference/controlP5/Button.html](http://www.sojamo.com/libraries/controlP5/reference/controlP5/Button.html)

---

<div class="post-metadata">

### Author: ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)
#### Post date: [February 19, 2020, 3:36pm UTC](https://discourse.processing.org/t/initializing-values-in-the-controlp5-button-library/17578/3 "2020-02-19T15:36:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [February 19, 2020, 4:31pm UTC](https://discourse.processing.org/t/initializing-values-in-the-controlp5-button-library/17578/4 "2020-02-19T16:31:04Z")

</div>

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

---

<div class="post-metadata">

### Author: ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)
#### Post date: [February 19, 2020, 5:01pm UTC](https://discourse.processing.org/t/initializing-values-in-the-controlp5-button-library/17578/5 "2020-02-19T17:01:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [March 5, 2020, 9:20pm UTC](https://discourse.processing.org/t/initializing-values-in-the-controlp5-button-library/17578/6 "2020-03-05T21:20:55Z")

</div>

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.

```auto
/**
 * 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:
 * https://discourse.processing.org/t/initializing-values-in-the-controlp5-button-library/17578/5
 */
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
  aButton.setValue(0)
    .setLabel("Open Output File")
    .setSize( 100, 20 )
    .setPosition(10, 10)
    .updateSize();
    
  // 3. register the callback -- see example from:
  // forum.processing.org/one/topic/controlp5-button-callback-runs-on-program-launch-without-mouse-click.html
  aButton.addCallback(
    new CallbackListener() {
    public void controlEvent(CallbackEvent event) {
      if (event.getAction() == ControlP5.ACTION_RELEASED) {
        mybutton();
      }
    }
  }
  );
}

void draw(){}

void mybutton () { 
  println("mybutton");
}

```
