A Motivating Example (Properties and Bindings) (JavaFX 2)

Let’s start with an example that shows off the capabilities of the Property interface through the use of a couple of instances of the SimpleIntegerProperty class.

Listing 3-1. MotivatingExample.java

MotivatingExample.java

 

 

 

 

MotivatingExample.java

 

 

 

 

MotivatingExample.java


In this example we created a SimpleIntegerProperty object called intProperty with an initial value of 1024. We then updated its value through a series of different integers while we added and then removed an InvalidationListener, added and then removed a ChangeListener, and finally created another SimpleIntegerProperty named otherProperty, bound it to and then unbound it from intProperty. The sample program used a generous amount of println calls to show what is happening inside the program.

When we run the program in Listing 3-1, the following output is printed to the console:

tmpA-107

 

 

 

tmpA-108

By correlating the output lines with the program source code (or by stepping through the code in the debugger of your favorite IDE), we can draw the following conclusions.

• A SimpleIntegerProperty object such as intProperty and otherProperty holds an int value. The value can be manipulated with the get(), set(), getValue(), and setValue() methods. The get() and set() methods perform their operation with the primitive int type. The getValue() and setValue() methods use the Integer wrapper type.

• You can add and remove InvalidationListener objects to and from intProperty.

• You can add and remove ChangeListener objects to and from intProperty.

• Another Property object such as otherProperty can bind itself to intProperty. When that happens, otherProperty receives the value of intProperty.

• When a new value is set on intProperty, whatever object that is attached to it is notified. The notification is not sent if the object is removed.

• When notified, InvalidationListener objects are only informed of which object is sending out the notification and that object is only known as an Observable.

• When notified, ChangeListener objects are informed on two more pieces of information—the oldValue and the newValue—in addition to the object sending the notification. The sending object is known as an ObservableValue.

• In the case of a binding property such as otherProperty, we cannot tell from the output when or how it is notified of the change of value in intProperty. However, we can infer that it must have known of the change because when we asked otherProperty for its value we get back the latest value of intProperty.

■ Note Even though this motivating example uses an Integer property, similar examples can be made to use properties based on the Boolean, Long, Float, Double, String, and Object types. In the JavaFX 2.0 properties and bindings framework, when interfaces are extended or implemented for concrete types, they are always done for the Boolean, Integer, Long, Float, Double, String, and Object types.

This example brings to our attention some of the key interfaces and concepts of the JavaFX 2.0 properties and bindings framework: including the Observable and the associated InvalidationListener interfaces, the ObservableValue and the associated ChangeListener interfaces, the get(), set(), getValue(), and setValue() methods that allow us to manipulate the values of a SimpleIntegerProperty object directly, and the bind() method that allows us to relinquish direct manipulation of the value of a SimpleIntegerProperty object by subordinating it to another SimpleIntegerProperty object.

In the next section we show you these and some other key interfaces and concepts of the JavaFX 2.0 properties and bindings framework in more detail.

Next post:

Previous post: