O
- the type of the observable instance (e.g ObservableValue
or
ObservableMap
) to which the listener will be addedL
- the type of the listener which will be added to the observablepublic final class ListenerHandleBuilder<O,L> extends Object
ListenerHandle
. Note that it is abstract enough to be used for all kinds of
observable/listener relation and not just for those occurring in JavaFX.
The created handle manages whether the listener is currently attached. The functions specified to
onAttach(BiConsumer)
and onDetach(BiConsumer)
are only called when necessary. This is the case
attach
is called when the listener is not currently added to the observable
detach
is called when the listener is currently added to the observable
The ListenerHandle
returned by this builder is not yet attached, i.e. it does not initially call the
functions given to onAttach
or onDetach
.
A typical use looks like this:
Property<String> textProperty; ChangeListener<String> textListener; ListenerHandle textListenerHandle = ListenerHandleBuilder .from(textProperty, textListener) .onAttach((property, listener) -> property.addListener(listener)) .onDetach((property, listener) -> property.removeListener(listener)) .build();Or, with method references:
Property<String> textProperty; ChangeListener<String> textListener; ListenerHandle textListenerHandle = ListenerHandleBuilder .from(textProperty, textListener) .onAttach(Property::addListener)) .onDetach(Property::removeListener) .build();
Modifier and Type | Method and Description |
---|---|
ListenerHandle |
buildAttached()
Creates a new listener handle and attaches the listener.
|
ListenerHandle |
buildDetached()
Creates a new, initially detached listener handle.
|
static <O,L> ListenerHandleBuilder<O,L> |
from(O observable,
L listener)
Creates a builder for a generic
ListenerHandle . |
ListenerHandleBuilder<O,L> |
onAttach(BiConsumer<? super O,? super L> add)
Sets the function which is executed when the built
ListenerHandle must add the listener because
attach was called. |
ListenerHandleBuilder<O,L> |
onDetach(BiConsumer<? super O,? super L> remove)
Sets the function which is executed when the built
ListenerHandle must remove the listener because
detach was called. |
public static <O,L> ListenerHandleBuilder<O,L> from(O observable, L listener)
ListenerHandle
.O
- the type of the observable instance (e.g ObservableValue
or
ObservableMap
) to which the listener will be addedL
- the type of the listener which will be added to the observableobservable
- the observable instance to which the listener
will be addedlistener
- the listener which will be added to the observable
ListenerHandleBuilder
for a ListenerHandle
.public ListenerHandleBuilder<O,L> onAttach(BiConsumer<? super O,? super L> add)
ListenerHandle
must add the listener because
attach
was called.
Because the built handle manages whether the listener is currently attached, the function is only called when
necessary, i.e. when attach
is called when the listener is currently not added to the observable.
add
- the BiConsumer
called on attach
; the arguments for the function are the observable and
listener specified during this builder's constructionpublic ListenerHandleBuilder<O,L> onDetach(BiConsumer<? super O,? super L> remove)
ListenerHandle
must remove the listener because
detach
was called.
Because the built handle manages whether the listener is currently attached, the function is only called when
necessary, i.e. when detach
is called when the listener is currently added to the observable.
remove
- the BiConsumer
called on detach
; the arguments for the function are the observable and
listener specified during this builder's constructionpublic ListenerHandle buildAttached() throws IllegalStateException
onAttach(BiConsumer)
and onDetach(BiConsumer)
have been called.ListenerHandle
; initially attachedIllegalStateException
- if onAttach(BiConsumer)
or onDetach(BiConsumer)
have not been calledpublic ListenerHandle buildDetached() throws IllegalStateException
onAttach(BiConsumer)
and
onDetach(BiConsumer)
have been called.ListenerHandle
; initially detachedIllegalStateException
- if onAttach(BiConsumer)
or onDetach(BiConsumer)
have not been calledThis documentation is licensed under CC-BY 4.0, attributed to Nicolai Parlog from CodeFX.