Java Reference
In-Depth Information
In this example, the
root
is an
ObjectProperty
that observes
Lighting
objects. The binding
colorBinding
observes the
color
property of the
light
property of the
Lighting
object that is the value of
root
. We then created
some
Light
and
Lighting
objects and changed their configuration in various ways.
When we run the program in Listing 4-15, the following output is printed to the console:
firstLight is black.
secondLight is white.
firstLighting has firstLight.
secondLighting has secondLight.
Making root observe firstLighting.
The color changed:
old color = 0xffffffff,
new color = 0x000000ff
Making root observe secondLighting.
The color changed:
old color = 0x000000ff,
new color = 0xffffffff
Changing secondLighting's light to firstLight
The color changed:
old color = 0xffffffff,
new color = 0x000000ff
Changing firstLight's color to red
The color changed:
old color = 0x000000ff,
new color = 0xff0000ff
As expected, a change event is fired for every change in the configuration of the object being observed by
root
,
and the value of
colorBinding
always reflects the color of the light of the current
Lighting
object in
root
.
■
the JavaFX properties and bindings framework does not issue any warnings if the supplied property names
do not match any property names in a JavaFX bean. it will simply have the default value for the type:
null
for object type,
zero for numeric types,
false
for
boolean
type, and the empty string for string type.
Caution
Adapting JavaBeans Properties to JavaFX Properties
Over the many years since the JavaBeans specification was published, a lot of JavaBeans were written for various
projects, products, and libraries. To better help Java developers leverage these JavaBeans, a set of adapters were
provided in the
javafx.beans.properties.adapter
package to make them useful in the JavaFX world by creating a
JavaFX property out of JavaBeans properties.
In this section, we first briefly review the JavaBeans specification definition of properties, bound properties,
and constrained properties by way of a simple example. We then show you how to create JavaFX properties out of
JavaBeans properties using the adapters.
Understanding JavaBeans Properties
JavaBeans
properties
are defined using the familiar getter and setter naming convention. A property is “read only” if
only a getter is provided, and it is “read/write” if both a getter and a setter are provided. A JavaBeans event is made
up of the event object, the event listener interface, and listener registration methods on the JavaBean. Two particular
Search WWH ::
Custom Search