A replace trigger is a block of code attached to a variable that is executed when the variable’s value changes. Here’s the example from the JavaFX Tutorial:
var password = "foo" on replace oldValue {
println("\nALERT! Password has changed!");
println("Old Value: {oldValue}");
println("New Value: {password}");
};
password = "bar";
When the script is run, it produces this output:
ALERT! Password has changed!
Old Value:
New Value: foo
ALERT! Password has changed!
Old Value: foo
New Value: bar
I wondered if triggers could be used to exclude certain values so I altered the code to experiment:
var password = "foo" on replace oldValue {
if (password == "bar") {
password = oldValue;
println("'bar' is not allowed");
} else {
println("\nALERT! Password has changed!");
println("Old Value: {oldValue}");
println("New Value: {password}");
}
};
password = 'bar';
password = 'password';
By running this version of the code, I confirmed a trigger can be used to filter input but I also discovered triggers are recursive:
ALERT! Password has changed!
Old Value:
New Value: foo
ALERT! Password has changed!
Old Value: bar
New Value: foo
'bar' is not allowed
ALERT! Password has changed!
Old Value: foo
New Value: password
When password = 'bar' is executed, the variable is changed to the new value and the trigger is fired. Since if (password == 'bar') evaluates to true, password is reset to oldValue. At this point, the trigger is fired again, the value is changed and the appropriate messages printed, then execution returns to print the message, “‘bar’ is not allowed.”
Nice to know info. This would definitely cause headaches if you didn’t know about it, especially during debugging.

