# Connect a function to a button created in an object (ControlP5)

**URL:** <https://discourse.processing.org/t/connect-a-function-to-a-button-created-in-an-object-controlp5/1821>\
**Category:** Libraries\
**Created:** [July 16, 2018, 4:19pm UTC](https://discourse.processing.org/t/connect-a-function-to-a-button-created-in-an-object-controlp5/1821 "2018-07-16T16:19:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [July 16, 2018, 4:19pm UTC](https://discourse.processing.org/t/connect-a-function-to-a-button-created-in-an-object-controlp5/1821/1 "2018-07-16T16:19:22Z")

</div>

Hello all,

I have a class named “Interface” in witch I’m creating some buttons.  
I want to execute a function when a button is pressed.  
The problem is since I want my class to be generic, I can’t write that peace of code inside my class, I need it to be in my main code.  
Is there a way, then, to connect a function to a button created inside an object.

The example below show what I would like to achieve :

```auto
Interface i;

void setup() {
  size(800, 600);
  i = new Interface(this);
  i.connectFunctionToBtn(functionToTrigger); //This is what I would like to write
}

void draw() {
  background(20);
}

void functionToTrigger() {
   println("Function triggered"); 
}

```

Then my class Interface would be something like this :

```auto
import controlP5.*;

class Interface {
 
  ControlP5 cp5;
  
  Interface(PApplet p_parent) {
    cp5 = new ControlP5(p_parent);
    
    cp5.addButton("btnTest")
    .setPosition(10, 10)
    .setSize(50,20);
  }
  
  // THE FUNCTION TO WRITE
  void connectFunctionToBtn() {
  }
}

```

Thank you for your help ! =D

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 16, 2018, 5:50pm UTC](https://discourse.processing.org/t/connect-a-function-to-a-button-created-in-an-object-controlp5/1821/2 "2018-07-16T17:50:26Z")

</div>

```auto
Interface i;
void setup() {
  size(800, 600);
  i = new Interface(this, "functionToTrigger");
}
void draw() {
  background(20);
}
void functionToTrigger() {
   println("Function triggered"); 
}

import controlP5.*;
class Interface {
  ControlP5 cp5;
  Interface(PApplet p_parent, String buttonTriggersThis) {
    cp5 = new ControlP5(p_parent);
    cp5.addButton(buttonTriggersthis)
    .setPosition(10, 10)
    .setSize(50,20);
  }
}

```

You could do this? I haven’t actually tried running this…

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [July 16, 2018, 5:53pm UTC](https://discourse.processing.org/t/connect-a-function-to-a-button-created-in-an-object-controlp5/1821/3 "2018-07-16T17:53:42Z")

</div>

Thanks a lot !

It is working like a charm 😃

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [July 16, 2018, 6:02pm UTC](https://discourse.processing.org/t/connect-a-function-to-a-button-created-in-an-object-controlp5/1821/4 "2018-07-16T18:02:22Z")

</div>

Just by curiosity, would there be a way of doing this without giving the name of the function to the button ?

It would be a bit more elegant in my project 😛

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 16, 2018, 6:05pm UTC](https://discourse.processing.org/t/connect-a-function-to-a-button-created-in-an-object-controlp5/1821/5 "2018-07-16T18:05:13Z")

</div>

In Processing, not really.  
In other languages you could be mucking about with function pointers.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [July 16, 2018, 6:19pm UTC](https://discourse.processing.org/t/connect-a-function-to-a-button-created-in-an-object-controlp5/1821/6 "2018-07-16T18:19:31Z")

</div>

Yep , I was thinking abot something like that.

Too bad 😕…

---

<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:** [July 16, 2018, 8:26pm UTC](https://discourse.processing.org/t/connect-a-function-to-a-button-created-in-an-object-controlp5/1821/7 "2018-07-16T20:26:27Z")

</div>

[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://github.com/sojamo/controlp5/blob/master/examples/extra/ControlP5ControllerInsideClass/ControlP5ControllerInsideClass.pde>

```auto
// 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 Interface(cp5 = new ControlP5(this));
}

void draw() {
  background(050);
}

class Interface {
  final ControlP5 cp5;

  Interface(final ControlP5 controlP5) {
    (cp5 = controlP5).addButton(getClass().getSimpleName())
      .setPosition(10, 10)
      .setSize(50, 20)
      .plugTo(this, "gotClicked");
  }

  void gotClicked() {
    println("You've clicked at me!");
  }
}

```
