Tuesday, August 18, 2009

JavaFX Script Data Binding and Trigger

Data Binding

Data binding refers to keeping the state in synch of two objects or variable. In JavaFX, one variable needs to bound to another and on change of state of one variable the other changes automatically.

//Variable declration
var x = 10;

//y is bound to x, that means when x changes y will automatically change.
var y = bind x;

println(x); // will print 10
println(y); // will print 10

x = 11;     //change the value of x
println(x); // will print 11
println(y); // will print 11
//y=12;     // if uncommented, will throw exception because bound variables cannot be changed


Trigger are piece of code that can be attached to a variable. The code is invoked when the value of the variable changes.

var a on replace oldValue{
    println("Value Changed from {oldValue} to {a}");

//change the value
a =6;

On execution it prints

Value Changed from 0 to 0
Value Changed from 0 to 6

More on JavaFX

No comments:

Post a Comment