Java Reference
In-Depth Information
■
the full-lazy instantiation strategy incurs the cost of an extra field to stave off the need for property
instantiation a little longer. similarly, both the half-lazy and the full-lazy instantiation strategies incur costs of
implementation complexity and runtime performance to gain the benefit of a potentially reduced runtime memory
footprint. this is a classical trade-off situation in software engineering. Which strategy you choose will depend on
the circumstance of your application. our advice is to introduce optimization only if there is a need.
Caution
Using Selection Bindings
As you saw in the “Understanding the Bindings Utility Class” section earlier, the
Bindings
utility class contains seven
selection operators. The method signatures of these operators are:
•
select(Object root, String... steps)
•
selectBoolean(Object root, String... steps)
•
selectDouble(Object root, String... steps)
•
selectFloat(Object root, String... steps)
•
selectInteger(Object root, String... steps)
•
selectLong(Object root, String... steps)
selectString(Object root, String... steps)
These selection operators allow you to create bindings that observe deeply nested JavaFX Beans properties.
Suppose that you have a JavaFX bean that has a property, whose type is a JavaFX bean that has a property, whose type
is a JavaFX bean that has a property, and so on. Suppose also that you are observing the root of this properties chain
through an
ObjectProperty
. You can then create a binding that observes the deeply nested JavaFX Beans property
by calling one of the select methods whose type matches the type of the deeply nested JavaFX Beans property with
the
ObjectProperty
as the root, and the successive JavaFX Beans property names that reach into the deeply nested
JavaFX Beans property as the rest of the arguments.
•
■
there is another set of select methods that takes an
ObservableValue
as the first parameter. they were
introduced in JavaFX 2.0. the set of select methods that takes an
Object
as the first parameter allows us to use any
Java object, not merely JavaFX Beans, as the root of a selection binding.
Note
In the following example, we use a few classes from the
javafx.scene.effect
package—
Lighting
and
Light
—to
illustrate how the selection operator works. We teach you how to apply lighting to a JavaFX scene graph in a later
chapter of the topic. For now, our interest lies in the fact that
Lighting
is a JavaFX bean that has a property named
light
whose type is
Light
, and that
Light
is also a JavaFX bean that has a property named
color
whose type is
Color
(in
javafx.scene.paint
).
The program in Listing 4-15 illustrates how to observe the color of the light of the lighting.
Listing 4-15.
SelectBindingExample.java
import javafx.beans.binding.Bindings;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
Search WWH ::
Custom Search