Processing alternative to inline onclick()

menu = new button({
  x:menuxy[0]+xx,
  y:menuxy[1]+yy-10,
  width:190+12,
  height:30*10,
  id:"menu",
  col:color(255,255,255,20),
  label:"",
  onClick: function(){
    setups();
  }
});

This is what I am converting from p5.js. Its a call to create a new button with the name “menu”. Now this works fine in p5.js however this does not seem possible in processing. The only functionality which I do not know how to replicate is the
onClick: function(){setups();}
Of course, I know how to create a onClick function, but the beauty of this bit of code, is that it enables you to assign different functions to different buttons created using the same “class” ( I know some might argue, this is not really a class in p5). Anyway is there some sort of processing alternative, I cannot seem to find any documentation which would suggest this, and if not, what would you suggest as an alternative?

2 Likes

ControlP5 has a built in mechanisms for this. The basic mode is callbacks. For example, you write a callback bang(), then create a controller with the callback label “bang”.

http://www.sojamo.de/libraries/controlP5/examples/controllers/ControlP5bang/ControlP5bang.pde

It also has a Behavior abstract class extension mechanism.

http://www.sojamo.de/libraries/controlP5/examples/use/ControlP5behavior/ControlP5behavior.pde

I believe that G4P does this as well. Both support full event models and control can get pretty fine-grained.

http://www.lagers.org.uk/g4p/ref/classg4p__controls_1_1_g_button.html