//rich client /

User interface (UI) com- ponents expose the
state of the business logic
to the user. A user’s input
changes the state of the
UI components first, but
at some point, the state of
the UI components needs
to be synchronized with
the underlying business
logic, which is often implemented as plain old Java
domain objects. This article
describes the data flow
between the presentation
and business logic realized
with the JavaFX API.

Note: The previous article
in this series described how
to integrate the JavaFX Scene
Builder output into multiview enterprise applications.

What Is Data Binding?

Data binding is the process
of automatic data synchronization between objects.

Data binding can be declarative (for example, the value
binding in JavaServer Faces
[JSF] is declarative) or programmatic, as discussed in
this article.

JavaFX comes with different data binding granu-larities. A high-level, coarse-grained, fluent API allows
chaining of calls, which
results in complex expressions. You can implement
string concatenations as well
as complex computations,
and you can evaluate the
expression in lazy or eager
fashion. The lower-level API
directly connects two properties and synchronizes the
state between them without
any further processing.

No Properties, No Binding

Java SE does not supportdata binding at the languagelevel. You cannot just con-nect two fields of aclass and expect amagical synchro-nization behindthe scenes. JavaFXcomes with classes,comparable toJavaBeans, thatprovide such magic.

You can even use
the JavaFX properties without launching a UI, as shown
in Listing 1.

Binding also
works bidirectionally; changes to one
part of the property
will be reflected in
the other parts (see

Listing 2).

Direct data bind-ing works onlybetween a javafx.beans. property.Property and ajavafx.beans.value.ObservableValue. To con-nect a JavaFXproperty with aJavaBean, you willhave to translatethe state changesinto a series ofmethod invoca-tions. This can beeasily achieved witha ChangeListener, asshown in Listing 3.

Any custom
implementation
of ObservableValue
could be used as
a binding target.

To demonstratethis, we will usea simple timercalled Pomodoro(see Listing 4),which fires at afixed schedule andpasses the current time tothe listeners. The Pomodorotechnique is sometimesused by programmers to