Two things here - one, davbol is right, that way of doing things is very Java, and it's often the way you do things in Swing. It allows you to just declare the method that you need inline (what you're doing there is creating a new implementation of the buttonAction interface and implementing the required doStuff() function inline, which is quite a bit more local than having to do it all in an externally declared class, which is why it is preferred).
While this is the "correct" way to handle these things, I still feel as if it can cause quite a bit of code bloat, and to date I've never really seen a good explanation of why it's better than using reflection - to me it still seems to be mostly just a hack (albeit an officially sanctioned one) to wedge things that don't fit into Java's preferred object model. So just to give you the choice, here's the way I'd do it (actually, this is ripped from http://code.google.com/p/phylowidget/ - Phylowidget, a Google Summer of Code project that my brother worked on).
Within your button class definition, add the following:
Code:
Object o;
Method m;
public void setAction(Object object, String method){
this.o = object;
if (method != null && !method.equals("") && o != null){
try{
m = o.getClass().getMethod(method, null);
} catch (SecurityException e){
e.printStackTrace();
} catch (NoSuchMethodException e){
e.printStackTrace();
}
}
}
public void performAction(){
if (m == null || o == null) return;
try{
m.invoke(o, null);
} catch (Exception e){
e.printStackTrace();
}
}
Then when you want to register a method to call with a button, you'd just call myButton.setAction(myObject,"doStuff"); (you might do this from within the constructor, in which case setAction does not even need to be public if you never want to change it).
To initiate the action, you'd call myButton.performAction(), probably from whatever code in the Button class you have that decides that the button has been clicked.
Lastly, if you need to access a method that is not declared within a class (i.e. at the top level in the Processing editor), then as long as you're doing so outside of other class definitions, you should be able to call myButton.setAction(this,"desiredMethodName"), which should pass the PApplet as the object, which will include the top level methods that you've declared. If you are inside another class, you may need to store a PApplet variable that you set equal to this in the top level, and then pass that. I haven't verified this, but I'm pretty sure it should work.
If you need to call methods with arguments, I'd take a look through Processing's PApplet.java, at http://dev.processing.org/source/index.cgi/trunk/processing/core/src/processing/core/PApplet.java?view=markup (search for "class RegisteredMethods" to get to the right piece of the file) and see the way it's done there.
For more info, check out http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html for the Javadoc on the Class class (you'll mainly be interested in the getMethod() function, and perhaps also look at the Method class), and browse a bit to see what else you may be able to use.