Java Reference
In-Depth Information
Chapter 4
Properties and Bindings
Heaven acts with vitality and persistence.
In correspondence with this
The superior person keeps himself vital without ceasing.
—I Ching
In earlier chapters, we introduced you to the JavaFX 8 platform that is part of Oracle JDK 8. You set up your
development environment with your favorite IDE: Eclipse, NetBeans, or IntelliJ IDEA. You wrote and ran your first
JavaFX GUI programs. You learned the fundamental building blocks of JavaFX: the
Stage
and
Scene
classes, and
the
Node
s that go into the
Scene
. You have no doubt noticed the use of user-defined model classes to represent the
application state and have that state communicated to the UI through properties and bindings.
In this chapter, we give you a guided tour of the JavaFX properties and bindings framework. After recalling a little
bit of history and presenting a motivating example that shows various ways that a JavaFX
Property
can be used, we
cover key concepts of the framework:
Observable
,
ObservableValue
,
WritableValue
,
ReadOnlyProperty
,
Property
,
and
Binding
. We show you the capabilities offered by these fundamental interfaces of the framework. We then show
you how
Property
objects are bound together, how
Binding
objects are built out of properties and other bindings—
using the factory methods in the
Bindings
utility class, the fluent interface API, or going low level by directly
extending abstract classes that implement the
Binding
interface—and how they are used to easily propagate changes
in one part of a program to other parts of the program without too much coding. We then introduce the JavaFX Beans
naming convention, an extension of the original JavaBeans naming convention that makes organizing your data
into encapsulated components an orderly affair. We finish this chapter by showing how to adapt old-style JavaBeans
properties into JavaFX properties.
Because the JavaFX properties and bindings framework is a nonvisual part of the JavaFX platform, the example
programs in this chapter are also nonvisual in nature. We deal with
Boolean
,
Integer
,
Long
,
Float
,
Double
,
String
,
and
Object
type properties and bindings as these are the types in which the JavaFX binding framework specializes.
Your GUI building fun resumes in the next and further chapters.
Forerunners of JavaFX Binding
The need for exposing attributes of Java components directly to client code, allowing them to observe and to
manipulate such attributes and to take action when their values change, was recognized early in Java's life. The
JavaBeans framework in Java 1.1 provided support for properties through the now familiar getter and setter
convention. It also supported the propagations of property changes through its
PropertyChangeEvent
and
PropertyChangeListener
mechanism. Although the JavaBeans framework is used in many Swing applications, its
use is quite cumbersome and requires quite a bit of boilerplate code. Several higher level data binding frameworks
were created over the years with various levels of success. The heritage of the JavaBeans in the JavaFX properties
and bindings framework lies mainly in the JavaFX Beans getter, setter, and property getter naming convention when
Search WWH ::
Custom Search