//rich client /
text. As you’ll see when we walk through
the relevant code, this demonstrates the
concept of lazy evaluation.
Also, when you click the Lap but-
ton, the lap time at that moment is
displayed. You may also click the Stop
button to cause the elapsed time to
stop increasing, and you may click the
Reset button to reset both the elapsed
and lap times.
displayed as an integer, rather than in
minutes:seconds:milliseconds format.
This is because LazyInitEvalSolution
uses a custom binding that you’ll implement in one of the steps below.
Below are the steps you can follow
to implement all of the behavior in
LazyInitEvalSolution.
tiates a SimpleStringProperty, binds it
to the elapsedMillisProperty property of
the Stop WatchModel class, and adds an
InvalidationListener to it.
As a result, when the value of
elapsed-TimeStrProperty is no longer valid, it isn’t
evaluated immediately; hence, the term
lazy evaluation.
Obtaining and Running the
LazyInitEvalExercise Project
1. Download the NetBeans project
file, which includes the
LazyInitEvalExercise program.
2. Expand the project into a directory of
your choice.
3. Start NetBeans, and select File ->
Open Project.
4. From the Open Project dialog box,
navigate to your chosen directory and
open the LazyInitEvalExercise project,
as shown in Figure 4. If you receive a
message stating that the jfxrt.jar file
can’t be found, click the Resolve button and navigate to the rt/lib folder
subordinate to where you installed
the JavaFX 2 SDK.
Note: You can obtain the NetBeans
IDE from the NetBeans site.
5. To run the application, click the Run
Project icon on the toolbar, or press
the F6 key. The icon looks like the Play
button on a DVD player, as shown in
Figure 5.
The LazyInitEvalExercise application
should appear in a window, as shown in
Figure 6.
The behavior of LazyInitEvalExercise
is different in a few ways from
LazyInitEvalSolution. For example, as
shown in Figure 6, clicking the Start button and then clicking the invalidate
text causes the elapsed time to be
Step 1: Gain an
Understanding of Lazy
Versus Eager Binding
Evaluation
A binding can be evaluated
in either an eager or a lazy
manner:
■ ■ When a binding is evaluated in an eager manner, the updated value of
the binding is calculated
whenever the binding is
invalidated.
■ ■ When a binding is evaluated in a lazy manner, the
updated value of the binding is calculated only
when needed.
Take a look at the code in
the Stop WatchNode.java file
in the LazyInitEvalExercise
project, which shows the
starter code for this example.
We’ll show code snippets
from LazyInitEvalMain.java
as you perform the steps in
this exercise.
Implementing lazy evaluation in a binding. As shown in
Listing 1, some of the code in
Stop WatchNode.java instan-
JAVA IN ACTION
Figure 4
Figure 5
ABOUT US
blog
Figure 3