# P5 Control in custom class

**URL:** <https://discourse.processing.org/t/p5-control-in-custom-class/27865>\
**Category:** Libraries\
**Created:** [February 16, 2021, 3:50pm UTC](https://discourse.processing.org/t/p5-control-in-custom-class/27865 "2021-02-16T15:50:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xbix](https://avatars.discourse-cdn.com/v4/letter/x/ec9cab/32.png) [@xbix](https://discourse.processing.org/u/xbix)\
**Post date:** [February 16, 2021, 3:50pm UTC](https://discourse.processing.org/t/p5-control-in-custom-class/27865/1 "2021-02-16T15:50:09Z")

</div>

I’m trying to add p5 UI to my drawing class, but when I add

```auto
cp5 = new ControlP5(this);

```

to the constructor I get:

"The constructor "ControlP5(myClassName) does not exist.

Loading p5 from the setup() works fine. What am I missing here?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 16, 2021, 4:14pm UTC](https://discourse.processing.org/t/p5-control-in-custom-class/27865/2 "2021-02-16T16:14:39Z")

</div>

Hi,

It’s because the `ControlP5()` constructor accepts the following arguments (from the [library’s javadoc](https://www.sojamo.de/libraries/controlP5/reference/index.html) ):

```auto
ControlP5(processing.core.PApplet theParent)
Create a new instance of controlP5.

```

So when you are using it in your class, the keyword `this` refers to the current instance of your class but not the `PApplet` which is the wrapper class of your Processing sketch.

Instead, you can eventually pass the `PApplet` instance to the constructor of your class :

```auto
import controlP5.*;

class MyClass {
  ControlP5 cp5;
  
  MyClass(PApplet parent) {
    cp5 = new ControlP5(parent);
  }
}

// Instanciate it
MyClass instance = new MyClass(this);

```

---

<div class="post-metadata">

**Author:** ![xbix](https://avatars.discourse-cdn.com/v4/letter/x/ec9cab/32.png) [@xbix](https://discourse.processing.org/u/xbix)\
**Post date:** [February 16, 2021, 4:35pm UTC](https://discourse.processing.org/t/p5-control-in-custom-class/27865/3 "2021-02-16T16:35:09Z")

</div>

Thank you a lot, also for the clear explication. got it working now!

But for some reason, the event Handler is not working anymore.  
I placed it in myClass:

```auto
void controlEvent(ControlEvent c) {
    println(c.getName() + "----" +c.getValue());
  }

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 16, 2021, 4:53pm UTC](https://discourse.processing.org/t/p5-control-in-custom-class/27865/4 "2021-02-16T16:53:35Z")

</div>

You are welcome! 😉

For the event handler, I think that it’s the reason behind passing the `PApplet` object to the constructor because they can now call some methods that you define in your main program.

If you put it in your class, then it’s no more accessible in the main `PApplet` class.

In the [source code](https://github.com/sojamo/controlp5), they check for a method called `controlEvent` :

```auto
// ControlP5.java

_myControlBroadcaster = new ControlBroadcaster( this );

// ControlBroadcaster.java

// ...

private String _myControllerCallbackEventMethod = "controlEvent";

// ...

_myControlEventPlug = checkObject( cp5.papplet , getEventMethod( ) , new Class[] { ControlEvent.class } );

// ...
protected static ControllerPlug checkObject( final Object theObject , final String theTargetName , final Class< ? >[] theAcceptClassList ) {
  // ...
  ControllerPlug cp = new ControllerPlug( CallbackEvent.class , theObject , theTargetName , ControlP5Constants.EVENT , -1 );
  // ...
}

// Then in ControllerPlug.java

/* check for methods */
		if ( _myType == ControlP5Constants.METHOD ) {
			try {
				Method[] myMethods = myClass.getMethods( );
              // ...

/* check for controlEvent */
		} else if ( _myType == ControlP5Constants.EVENT ) {
			try {

				_myMethod = _myObject.getClass( ).getMethod( _myName , new Class[] { _myEventMethodParameter } );
				_myMethod.setAccessible( true );
				_myParameterClass = _myEventMethodParameter;

// And so on...

```

I don’t know if it’s possible to specify a custom class with an event handler… 🤔

---

<div class="post-metadata">

**Author:** ![xbix](https://avatars.discourse-cdn.com/v4/letter/x/ec9cab/32.png) [@xbix](https://discourse.processing.org/u/xbix)\
**Post date:** [February 18, 2021, 8:57am UTC](https://discourse.processing.org/t/p5-control-in-custom-class/27865/5 "2021-02-18T08:57:51Z")

</div>

Thanks again Josephh for your time!

So maybe my approach is just wrong.  
If I leave the event listener outside the class and instantiate p5 inside the class, the UI works but after a while, I get many errors in the console, and Java crashes.

I’m trying to create a class that has its own UI. What would be the right approach here?

Thanks!

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 18, 2021, 5:00pm UTC](https://discourse.processing.org/t/p5-control-in-custom-class/27865/6 "2021-02-18T17:00:38Z")

</div>

> [@xbix](#):
>
> What would be the right approach here?

Maybe you can pass an instance of the `controlP5` class to all of your classes, it’s the same principle with the previous example I wrote with the `PApplet`.

So in the constructor of your class, you can create all your widgets with the instance of conrolP5.
