Java Reference
In-Depth Information
This recipe creates a simple contact ( Contact ) object containing a first name and last
name. Notice the instance variables using the SimpleStringProperty class.
Many of these classes, which end in Property , are
javafx.beans.Observable classes that can all be bound. In order for these
properties to be bound, they must be the same data type. In the preceding example, you
create the first name and last name variables of type SimpleStringProperty out-
side the created Contact domain object. Once they have been created, you bind them
bidirectionally to allow changes to update on either end. So if you change the domain
object, the other bound properties are updated. And when the outside variables are
modified, the domain object's properties are updated. The following demonstrates bid-
irectional binding against string properties on a domain object ( Contact ):
Contact contact = new Contact("John", "Doe");
StringProperty fname = new SimpleStringProperty();
fname.bindBidirectional(contact.firstNameProperty());
StringProperty lname = new SimpleStringProperty();
lname.bindBidirectional(contact.lastNameProperty());
Next up is how to bind numbers. Binding numbers is simple when using the Fluent
API. This high-level mechanism allows developers to bind variables to compute values
using simple arithmetic. Basically, a formula is “bound” to change its result based on
changes to the variables it's bound to. Look at the Javadoc ( ht-
tp://docs.oracle.com/javase/8/javafx/api/javafx/beans/
binding/Bindings.html ) for details on all the available methods and number
types. In this example, you simply create a formula for an area of a rectangle. The area
( NumberBinding ) is the binding, and its dependencies are the width and height
( IntegerProperty ) properties. When binding using the fluent interface API, you'll
notice the multiply() method. According to the Javadoc, all property classes inher-
it from the NumberExpressionBase class, which contains the number-based flu-
ent interface APIs. The following code snippet uses the fluent interface API:
// Area = width * height
final IntegerProperty width = new
SimpleIntegerProperty(10);
final IntegerProperty height = new
SimpleIntegerProperty(10);
NumberBinding area = width.multiply(height);
Search WWH ::




Custom Search