Using binding operations
Directly connecting properties is not always enough. The Binding API can build complex dependencies between properties.
String operations
Concatenation is the most common string operation. It's called by one property of the String
type and takes another property of any type or a constant that will be used as a String,
as shown in the following code snippet:
// chapter3/operations/ConcatBinding.java
label.textProperty().bind(
stage.widthProperty().asString() // property one
.concat(" : ") // concatting with a string constant
.concat(stage.heightProperty()) // concatting with a property 2
);
Note
You don't need to call asString()
for a concat()
parameter, as it's done by JavaFX.
Note two very important concepts here:
- Binding operations allow us to chain them one after another (as in the Builder pattern, or in the Java8 Streams API)
- We've bound one property to the two others here
And, we are not limited by two properties: you can build a binding...