Button-problem method()

edited December 2015 in How To...

I started a thread some time ago asking for neat ways to code buttons (https://forum.processing.org/two/discussion/13157/what-s-your-favourite-way-to-code-a-button#latest) And There were two great answers that used method() Which I didn't even knew existed. But is there a way to use method() or something similar to access a method inside a class?


  • edited December 2015 Answer ✓
    • Both method() & thread() can only invoke functions at the sketch's top level.
    • That is, we can't directly invoke some class' method via them.
    • But nothing disallows that once inside the invoked function, that function invokes another method.
    • Just be aware that we can't pass arguments to either method() or thread().


  • edited December 2015

    Ok, the only problem with that is that if I have 50 different buttons I will have to have 50 functions in the first tab, which is annoying and a really roundabout way of doing it...

    But if there is no other way I'l just have to work with it :)

  • edited January 2016 Answer ✓

    If Button is some class, you'd just have to figure out which 1 was clicked at.
    Once found, call its class method and you're done.

    However, if each Button is tasked to do a diff. thing, you're gonna need a diff. technique too.

    Let's name that method action(). Let's say you've got 3 Button objects.
    1st Button is tasked to draw a rect(). 2nd an ellipse() and 3rd a triangle().

    At the moment we instantiate each of those 3, we @Override their action() method.
    Even though all those 3 got a method called action(), each action()'s gonna act differently from each other! :ar!

    Pay specially close attention within class Button, the abstract method action() demands to be implemented lest the whole class can't be instantiated!

    The following example is a modification of this online sketch below:

    And from this old forum thread below:

     * Overriden Action Buttons (v1.02)
     * by GoToLoop (2015-Dec-17)
     * forum.Processing.org/two/discussion/14039/button-problem-method
     * forum.Processing.org/two/discussion/558/creating-a-next-page-button
     * studio.ProcessingTogether.com/sp/pad/export/ro.9eDRvB4LRmLrr
    static final int BUTTONS = 3, GAP = 50, BTN_W = 80, BTN_H = 50;
    final Button[] btns = new Button[BUTTONS];
    Button btnAct;
    void setup() {
      size(600, 400);
    void draw() {
      for (Button b : btns)  b.display();
      if (btnAct != null)  btnAct.action();
    void mousePressed() {
      btnAct = null;
      for (Button b : btns)  if (b.isMouseInside()) {
        btnAct = b;
      redraw = true;
    void createButtons() {
      final int y = height - BTN_H - GAP;
      btns[0] = new Button(GAP, y, BTN_W, BTN_H, #FF0000) {
        @ Override public void action() {
          rect(width/2 - RAD, GAP*2 - RAD, DIM, DIM);
      btns[1] = new Button(width - BTN_W >> 1, y, BTN_W, BTN_H, #008000) {
        @ Override public void action() {
          ellipse(width>>1, GAP<<1, DIM, DIM);
      btns[2] = new Button(width - BTN_W - GAP, y, BTN_W, BTN_H, #0000FF) {
        @ Override public void action() {
          final int cx = width>>1;
          triangle(cx, GAP*2 - RAD, cx - RAD, GAP*2 + RAD, cx + RAD, GAP*2 + RAD);
    abstract class Button {
      static final int DIM = 150, RAD = DIM>>1;
      final int x, y, w, h, c;
      Button(int xx, int yy, int ww, int hh, color cc) {
        x = xx;
        y = yy;
        w = ww;
        h = hh;
        c = cc;
      abstract void action();
      void display() {
        rect(x, y, w, h);
      boolean isMouseInside() {
        return mouseX > x & mouseX < x+w & mouseY > y & mouseY < y+h;
  • edited June 2016

    Alternatively, give each button an unique id

    When clicked, read the id of the clicked Button

    write in a new tab a function doCommand() and pass it the id


    switch (id) {
        case 0:
        Case 1:
  • Ok, GoToLoop, so you instantiate the buttons in a new method/function and when you do that, you also defines what the method action() is going to do? That's actually pretty neat. But you have to instantiate it in a new method/function and not in setup?

    Thanks a lot for the help guys, this makes things much easier.

  • Answer ✓

    It's neat

    It's better in a extra function but also possible in setup()

  • edited December 2015 Answer ✓

    But you have to instantiate it in a new method/function and not in setup()?

    createButtons() is called from setup().
    Once createButtons() is finished, it goes back to setup(), which has called it.
    Of course you can pick createButtons()'s content and dump it all into setup().
    But that's not very modular & it's against "separation of concerns" (SoC):

Sign In or Register to comment.