Reputation: 7255
I am using this code:
renameWindow.showingProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
//Remove the Invalidator Listener
renameWindow.showingProperty().removeListener(this);
}
});
And i want to do in lambda way and i use:
renameWindow.showingProperty().addListener(listener->{
renameWindow.showingProperty().removeListener(this);
});
and i am getting error cause maybe the listener is the Observable interface or what?.I want to do this using lambda expression.How can this be done?How to remove the InvalidationListener using lambda.
Upvotes: 1
Views: 1382
Reputation: 82461
this
refers to a instance of the class containing the code, not to the lambda expression itself.
Unlike code appearing in anonymous class declarations, the meaning of names and the
this
andsuper
keywords appearing in a lambda body, along with the accessibility of referenced declarations, are the same as in the surrounding context (except that lambda parameters introduce new names).
Therefore you're trying to call removeListener
with a object as parameter that doesn't implement InvalidationListener
or ChangeListener
, which means none of the removeListener
methods is applicable.
The only way to get a reference to the lambda expression is by using some expression access a reference to it from the lambda body that is evaluated at the time the lambda body is executed.
This could be done e.g. by assigning it to a field.
Example
private InvalidationListener listener = observable -> renameWindow.showingProperty().removeListener(this.listener);
Upvotes: 2