# G4P - Defining handleButtonEvents(..) in a Class

**URL:** https://discourse.processing.org/t/g4p-defining-handlebuttonevents-in-a-class/18262
**Category:** Libraries
**Created:** [March 1, 2020, 2:17am UTC](https://discourse.processing.org/t/g4p-defining-handlebuttonevents-in-a-class/18262 "2020-03-01T02:17:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![MauiMana](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@MauiMana](https://discourse.processing.org/u/MauiMana)
#### Post date: [March 1, 2020, 2:17am UTC](https://discourse.processing.org/t/g4p-defining-handlebuttonevents-in-a-class/18262/1 "2020-03-01T02:17:45Z")

</div>

I’m coding _native_ java classes using the Processing 3 IDE. Using G4P as my GUI library I’m trying to encapsulate one of my logical GUI components inside its own class, including the event handler for associated events. However, when I define a by-control-type event handler within this class G4P doesn’t see it and only appears to be looking for such event handlers within the main processing ‘_implied_’ class. Am I perhaps missing something as to how to have such event handlers defined within other classes of my program?

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [March 1, 2020, 9:55am UTC](https://discourse.processing.org/t/g4p-defining-handlebuttonevents-in-a-class/18262/2 "2020-03-01T09:55:22Z")

</div>

G4P allows you to specify the _ **object** _ and the _ **class instance method** _ to be used to handle events from a particular button. The following sketch demonstrates this

```auto
import g4p_controls.*;

Foo foo;

GButton btn;

void setup() {
  size(300, 300);
  fill(255);
  textSize(60);
  // Create object to handle events from the button
  foo = new Foo();
  btn = new GButton(this, 10, 10, 100, 40, "Click Me!");
  btn.addEventHandler(foo, "buttonClick");
}

void draw() {
  background(32);
  text(foo.clickCount(), 100, 150);
}

// Make this a top-level class
public static class Foo {
  private int clickCount = 0;

  public Foo() {
  }

  // Event handler for a G4P button
  public void buttonClick(GButton button, GEvent event) { 
    clickCount++;
  }

  public int clickCount() {
    return clickCount;
  }
}

```

---

<div class="post-metadata">

### Author: ![MauiMana](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@MauiMana](https://discourse.processing.org/u/MauiMana)
#### Post date: [March 1, 2020, 7:32pm UTC](https://discourse.processing.org/t/g4p-defining-handlebuttonevents-in-a-class/18262/3 "2020-03-01T19:32:19Z")

</div>

Thank you for such a timely response, and on a weekend too. I see my mistake, I didn’t read the API close enough and simply assumed that the first argument to addEventHandler(…) needed to be a _sketch_ like many of the others. But with your example and rereading the reference doc I see that it is actually meant to be the object containing the callback. This solves my issue, thank you. Also, thanks for contributing such a nice GUI library to the processing community, I’m finding it very useful.
