From the docs of ObservableValue.addListener:
"If the same listener is added more than once, then it will be notified more than once."
Ask yourself this: Would there ever, in any part of the universe be a reason to add the same listener twice to the same property?
I suggest improving this by changing the addListener method to always ensure no duplicates are added.
My current workaround is to invoke removeListener before invoking addListener, e.g.
someProperty.removeListener(changeListener);
someProperty.addListener(changeListener);
I recommend changing JavaFX to no longer add duplicates, allowing me to simply write
someProperty.addListener(changeListener);
Alternatively, the API could be improved by adding an isListener method which would allow me to do the check for you, however, I believe this is the least preferable approach, since I would then have to write the following code over and over again:
if(!someProperty.isListener(changeListener))
someProperty.addListener(changeListener);
Still, even this less favourable solution would be better than the remove-then-add solution we have today.
"If the same listener is added more than once, then it will be notified more than once."
Ask yourself this: Would there ever, in any part of the universe be a reason to add the same listener twice to the same property?
I suggest improving this by changing the addListener method to always ensure no duplicates are added.
My current workaround is to invoke removeListener before invoking addListener, e.g.
someProperty.removeListener(changeListener);
someProperty.addListener(changeListener);
I recommend changing JavaFX to no longer add duplicates, allowing me to simply write
someProperty.addListener(changeListener);
Alternatively, the API could be improved by adding an isListener method which would allow me to do the check for you, however, I believe this is the least preferable approach, since I would then have to write the following code over and over again:
if(!someProperty.isListener(changeListener))
someProperty.addListener(changeListener);
Still, even this less favourable solution would be better than the remove-then-add solution we have today.
- relates to
-
JDK-8097874 ObservableValue should have a hasListener(listener) method
-
- Closed
-