Java Reference
In-Depth Information
■
an invalidation event is fired only once by any of the implementations of the
Property
interface in the JavaFX
properties and bindings framework if you call the setter with the same value several times in a row.
Note
Another place where invalidation events are fired is from
Binding
objects. You haven't seen an example of a
Binding
object yet, but there are plenty of
Binding
objects in the second half of this chapter. For now we just note that
a
Binding
object may become invalid, for example, when its
invalidate()
method is called or, as we show later in
this chapter, when one of its dependencies fires an invalidation event.
■
an invalidation event is fired only once by any of the implementations of the
Binding
interface in the JavaFX
properties and bindings framework if it becomes invalid several times in a row.
Note
Understanding the ObservableValue Interface
Next up in the hierarchy is the
ObservableValue
interface. It's simply an
Observable
that has a value. Its
getValue()
method returns its value. The
getValue()
method that we called on the
SimpleIntegerProperty
objects in the
motivating example can be considered to have come from this interface. You can register
ChangeListener
objects to
an
ObservableValue
object to receive
change
events.
You saw change events being fired in the motivating example in the last section. When the change event fires, the
ChangeListener
receives two more pieces of information: the old value and the new value of the
ObservableValue
object.
■
a change event is fired only once by any of the implementations of the
ObservableValue
interface in the
JavaFX properties and bindings framework if you call the setter with the same value several times in a row.
Note
The distinction between an invalidation event and a change event is made so that the JavaFX properties and
bindings framework may support
lazy evaluations
. We show an example of this by looking at three lines of code from
the motivating example:
otherProperty.bind(intProperty);
intProperty.set(7168);
System.out.println("otherProperty.get() = " + otherProperty.get());
When
intProperty.set(7168)
is called, it fires an invalidation event to
otherProperty
. On receiving this
invalidation event,
otherProperty
simply makes a note of the fact that its value is no longer valid. It does not
immediately perform a recalculation of its value by querying
intProperty
for its value. The recalculation is performed
later when
otherProperty.get()
is called. Imagine if instead of calling
intProperty.set()
only once as in the
preceding code we call
intProperty.set()
multiple times;
otherProperty
still recalculates its value only once.
■
the
ObservableValue
interface is not the only direct subinterface of
Observable
. there are four other
direct subinterfaces of
Observable
that live in the
javafx.collections
package:
ObservableList
,
ObservableMap
,
ObservableSet
, and
ObservableArray
with corresponding
ListChangeListener
,
MapChangeListener
,
SetChangeListener
, and
ArrayChangeListener
as callback mechanisms. these JavaFX observable collections are
covered in Chapter 7, “Collections and Concurrency.”
Note
Search WWH ::
Custom Search