How-To's > How do I use binding?
Use the bind keyword to create a relationship between a target variable and a bound expression. The bound expression can be a value of some basic type, an object, the outcome of a function, or the outcome of an expression.
Example Code
The following basic code creates a binding relationship between two variables:
var x = 0; def y = bind x;
The following code fragment shows a binding relationship between a variable and the outcome of a conditional expression:
Text {
translateX: 8
translateY: 20
content: bind if (replayTimeline.running) then " Stop" else "Replay"
}
The following code fragment shows a binding relationship between a variable and the outcome of a function:
enabled: bind enableNextButton(
products,
pageIndex,
productInfoView.product.index,
productInfoView.visible)
Tips
- Note that variable
yin the first code fragment is declared with thedefkeyword. This way, no code can directly assign a value to this variable, although its value can be changed by thebindoperator. Using thedefkeyword to define variables improves performance slightly and should be preferred when it can be used.
Examples
- Building GUI Applications With JavaFX, Lesson 5: Applying Data Binding to UI Objects
This tutorial provides a simple example and description of binding. - Learning the JavaFX Script Programming Language, Lesson 8: Data Binding and Triggers
This tutorial describes the syntax of binding in the JavaFX Script programming language. - Playing Video on the Sides of a Rotating Cube
The sample shows various instances of data binding. Check out its code to see data binding between two variables, a variable and the outcome of an expression, and a variable and a sequence. - Shopping Service
This sample accesses Yahoo's shopping API from JavaFX and demonstrates data binding between a variable and the outcome of a function.
Last Updated: April 2010
[Return to How-To's Home]