Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call function on button click?

I have a Processing class, called Button, which has the following function onClick:

class Button() {
    // Extra code
    void onClick() {
        // Run function here
    }
}

which is supposed to trigger a function call when the button is clicked, and the function is taken in from an argument, like so:

Button test = new Button(..., callFunction())

Where callFunction() is guaranteed to be a void function. How do I call the function callFunction() once test is clicked on? I can check if the button is clicked, but I have no idea how to wire that to a function call provided by an argument.

like image 653
Qwerp-Derp Avatar asked Dec 12 '25 00:12

Qwerp-Derp


1 Answers

Use Runnable.

Button test = new Button(..., new Runnable(){

     public void run() {
         // your stuff.
         callFunction();
     }
});

Then:

class Button() {

    private Runnable runnable;

    public Button (... Runnable runnable ){
       this.runnable  = runnable;
    }

    // Extra code
    void onClick() {
        runnable.start();
    }
}
like image 130
Mariano L Avatar answered Dec 14 '25 14:12

Mariano L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!