We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I just created a Button class for my current project, and I'd like a way to assign a function to each button created.
It would be most useful to store the function as a variable upon calling the constructor and later call that function when a click is detected inside the Button. After searching the forums I see it's rather difficult to do this, and it's also a bad idea to store the function name as a String and use that to call it.
I'd like to avoid hard coding the functions for each button into a clickHandler function. Do you guys have suggestions on what to do?
Thanks
Answers
https://forum.Processing.org/two/discussion/14039/button-problem-method
This is clever, thanks
Quick question, in your solution, why is it necessary for action() to be abstract? Can you only override abstract methods?
abstract
is just something fancy which obliges us to implement all methods marked as such when we try to instantiate or extend its class.abstract
class is an incomplete class. That is, it can have incompleteabstract
methods.abstract
.abstract void action();
w/void action() {}
.new Button()
like:btns[0] = new Button(GAP, y, BTN_W, BTN_H, #FF0000) {};
abstract
classes can't be instantiated w/ justnew
.abstract
mark from action():void action() {}
P.S.: Notice that in order to directly instantiate an
abstract
class we need to at least add{};
after
new
likenew Button() {};
.However do not add
{};
for regular classes. It ends up instantiating them as if they were some derived class for no reason or gain:new PVector() {};
// WRONG! Derived class object.new PVector();
// RIGHT! Real PVector object.This is incredibly helpful, thanks so much!