# Controlp5 - Multiple buttons that use one function

**URL:** <https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638>\
**Category:** Libraries\
**Created:** [October 19, 2018, 2:45pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638 "2018-10-19T14:45:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zac-04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zac-04/32/2009_2.png) [@Zac-04](https://discourse.processing.org/u/Zac-04)\
**Post date:** [October 19, 2018, 2:45pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/1 "2018-10-19T14:45:01Z")

</div>

So let’s say I had a class that’s display was a button.  
How would I make it so that all of these classes’ buttons point to one function.

e.g.

```auto
//In my class:
button = cp5.addButton("doThis");

//global scope:
void doThis() {
    print("Hello, world!");
}

//the error would obviously be this:
Oct 19, 2018 3:52:03 PM controlP5.ControlP5 checkName
WARNING: Controller with name "/doThis" already exists. overwriting reference of existing controller.

```

I understand why I get an error but I can’t think of an alternative, I guess what I really want is a separate function and name.

Thanks

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [October 19, 2018, 3:49pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/2 "2018-10-19T15:49:26Z")

</div>

Do you mean something like this?

```auto
import controlP5.*;

ControlP5 cp5;

void setup() {
  size(800, 400);
  cp5 = new ControlP5(this);
  cp5.addButton("A").setPosition(40, 40);
  cp5.addButton("B").setPosition(40, 60);
  cp5.addButton("C").setPosition(40, 80);
}

void draw() {
  background(0);
}

void controlEvent(CallbackEvent event) {
  if (event.getAction() == ControlP5.ACTION_CLICK) {
    switch(event.getController().getAddress()) {
    case "/A":
      println("Button A Pressed");
      break;
    case "/B":
      println("Button B Pressed");
      break;
    case "/C":
      println("Button C Pressed");
      break;
    }
  }
}

```

It’s based on the example called `ControlP5callback`.

---

<div class="post-metadata">

**Author:** ![Zac-04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zac-04/32/2009_2.png) [@Zac-04](https://discourse.processing.org/u/Zac-04)\
**Post date:** [October 19, 2018, 3:53pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/3 "2018-10-19T15:53:36Z")

</div>

No I’m afraid I mean if every button had the same ‘case’.

Thanks anyway.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [October 19, 2018, 4:01pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/4 "2018-10-19T16:01:15Z")

</div>

You need a way to distinguish the buttons in the code. But they can have the same label (so they look identical):

```
cp5.addButton("A").setLabel("A").setPosition(40, 40);
cp5.addButton("B").setLabel("A").setPosition(40, 60);
cp5.addButton("C").setLabel("A").setPosition(40, 80);
```

---

<div class="post-metadata">

**Author:** ![Zac-04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zac-04/32/2009_2.png) [@Zac-04](https://discourse.processing.org/u/Zac-04)\
**Post date:** [October 19, 2018, 4:02pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/5 "2018-10-19T16:02:44Z")

</div>

Also not what I mean, but I have an alternative to my problem because I am seeing that what I want isn’t possible: [Controlp5 - Buttons with private functions](https://discourse.processing.org/t/controlp5-buttons-with-private-functions/4641) by using a private function.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [October 19, 2018, 4:04pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/6 "2018-10-19T16:04:27Z")

</div>

Ok, I tried 🙂 Maybe you can share what you were trying to do and how you solved it, so others can benefit from your discovery 🙂

---

<div class="post-metadata">

**Author:** ![Zac-04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zac-04/32/2009_2.png) [@Zac-04](https://discourse.processing.org/u/Zac-04)\
**Post date:** [October 19, 2018, 4:04pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/7 "2018-10-19T16:04:54Z")

</div>

I will do when I find my solution 🙂

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 19, 2018, 4:08pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/8 "2018-10-19T16:08:44Z")

</div>

> [@Connect a function to a button created in an object (ControlP5)](https://discourse.processing.org/t/connect-a-function-to-a-button-created-in-an-object-controlp5/1821/7):
>
> [http://www.Sojamo.de/libraries/controlP5/reference/controlP5/Controller.html#plugTo-java.lang.Object-java.lang.String-](http://www.Sojamo.de/libraries/controlP5/reference/controlP5/Controller.html#plugTo-java.lang.Object-java.lang.String-) // https://Discourse.processing.org/t/ // connect-a-function-to-a-button-created-in-an-object-controlp5/1821/7 // https://GitHub.com/sojamo/controlp5/blob/master/examples/extra/ // ControlP5ControllerInsideClass/ControlP5ControllerInsideClass.pde // GoToLoop (2018/Jul/16) import controlP5.ControlP5; ControlP5 cp5; Interface i; void setup() { size(300, 200); i = new I…

---

<div class="post-metadata">

**Author:** ![Zac-04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zac-04/32/2009_2.png) [@Zac-04](https://discourse.processing.org/u/Zac-04)\
**Post date:** [October 19, 2018, 4:12pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/9 "2018-10-19T16:12:10Z")

</div>

I’m not sure how that relates to my problem, please elaborate.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 19, 2018, 4:15pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/10 "2018-10-19T16:15:39Z")

</div>

I believe you can point all your buttons to the same function via **plugTo()**. Didn’t test it though. 🤪

---

<div class="post-metadata">

**Author:** ![Zac-04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zac-04/32/2009_2.png) [@Zac-04](https://discourse.processing.org/u/Zac-04)\
**Post date:** [October 19, 2018, 4:30pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/11 "2018-10-19T16:30:24Z")

</div>

No I don’t think .plugTo works with buttons, my alternate problem is this one: [Controlp5 - Buttons with private functions](https://discourse.processing.org/t/controlp5-buttons-with-private-functions/4641)

---

<div class="post-metadata">

**Author:** ![Zac-04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zac-04/32/2009_2.png) [@Zac-04](https://discourse.processing.org/u/Zac-04)\
**Post date:** [October 23, 2018, 9:06pm UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/12 "2018-10-23T21:06:40Z")

</div>

I got found a solution, I had the buttons all be called `"doThis" + UID` and then added some code that tested if ‘doThis’ was in the name on the listener and ran the code like that.

---

<div class="post-metadata">

**Author:** ![GratefulDeadpool](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@GratefulDeadpool](https://discourse.processing.org/u/GratefulDeadpool)\
**Post date:** [October 30, 2020, 3:40am UTC](https://discourse.processing.org/t/controlp5-multiple-buttons-that-use-one-function/4638/13 "2020-10-30T03:40:38Z")

</div>

Hey there, I’m running into a similar issue:

I have an object that generates buttons with a method, but I want all of those buttons to do the same thing. There is not a finite amount of buttons that could be generated, so I can’t just create a bunch of similar functions with different names to be linked to buttons with different names.

Anyways, I’m a bit confused by your solution, do you mind elaborating on it? Do you perchance have your original code on hand? Any help would be greatly appreciated 🙂
