Reputation: 55
I am making a music application and it involves a slider at the bottom. To fix another problem I had with the listener not being active for some reason, I had to make it so the listener was added whenever an event occurred as there was nowhere else to add it. How can I remove an invalidation listener from an object to then add another one so that I don't have loads of invalidation listeners?
The listener looks something like this:
someMediaPlayer.currentTimeProperty().addListener(new InvalidationListener()
{
public void invalidated(Observable ov) {
someMethod();
}
});
Upvotes: 0
Views: 162
Reputation: 627
With the code you posted you will never be able to remove it as you do not store a reference. Every time the new InvalidationListener()
is called you will get a fresh reference. Instead store that InvalidationListener object and then you can remove it.
InvalidationListener listener = new InvalidationListener()
{
public void invalidated(Observable ov) {
someMethod();
}
};
someMediaPlayer.currentTimeProperty().addListener(listener);
....
someMediaPlayer.currentTimeProperty().removeListener(listener);
Upvotes: 3