Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
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:
unnable 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 :)
cboyd:
Something like this:unnable r = () -> System.out.println( "I'm a runnable button." ); Button b = new Button(); b.addClickListener( (e) -> r.run());
I'm a little bit lost in the line "(e) -> r.run()" how would I do this with Java 7?
My current state is:
unnable 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