How to extend a Button.ClickListener

Hey,

I’m a little bit lost (maybe cause it’s late and friday :)).
I’ve created a class which has a public method “.addSaveClickListener(ClickListener listener)”. However, I need to let the user provide a “Button.ClickListener” and the class always needs to add some code to the user’s given sequence.
So the user of my class creates and instance and calls “.addSaveClickListener(new Button.ClickListener { …” and creates a buttonClick function from the interface. This function is filled with some code and I (always) need to extend this code by a line of myself.
What would be the best practice to achieve this?

Thanks and regards

Hi OddDeer,

have you considered using a runnable, and invoking the runnable in your ClickListener implementation?

Something like this:

            Runnable r = () -> System.out.println( "I'm a runnable button." );
            Button b = new Button();
            b.addClickListener( (e) -> r.run()); 

Hi cboyd,

thanks for your input! I guess that this is what I was looking for - so thank you :slight_smile:

I’m a little bit lost in the line “(e) → r.run()” how would I do this with Java 7?

My current state is:

Runnable r_closer = new Runnable() {

@Override
public void run() {
System.out.println("This works");
}

};
//getB_save is a button (listener is the argument for the method)
this.getB_save().addClickListener(listener);

I’ve changed my JRE to Java 8. However, if I do this:

this.getB_save().addClickListener( (asdf) -> r_closer.run());

I get this error on asdf:

Lambda expression's parameter asdf cannot redeclare another local variable defined in an enclosing scope.

Hi OddDeer,

the error is telling you that you already have a variable named
asdf
. You should change the
(asdf)
to another name that you aren’t using