JavaFX: "on invalidate" and "on replace"
Friday, April 30, 2010 6:24:10 PM
"on invalidate" and "on replace".
The difference between on invalidate and on replace is that "on invalidate" is lazy (as in lazy binding) and "on replace" is eager. This will also have an influence if you bind a variable containing one of those statements.
You can use these statements after all kind of variable declaration, it doesn't matter if the variable has a start value or not or if it's bound to some variable or not:
var selectedId:Integer = 20 on invalidate {
updateSelection(this.selectedId);
}
You can always trigger the "on invalidate" of a variable (here selectedId) with
invalidate selectedId, but usually you won't have to do this.
Another usage I found out is maybe not what the inventor intended, but it's very practical:
var node:MapNode = bind controller.lineNodes[nodeList.selectedIndex] on invalidate {
nameInput.text = this.node.node;
var type = this.node.type;
typeSelect.select(type);
}
In this example, the variable only works as a trigger for "on validate", but it does this very well.
You can do almost anything inside the brackets, it behaves like a function. But you have to keep in mind one thing: It's a bad idea to assign a value to the variable which triggered the invalidation or replacement function. That's maybe why they hid the variable from its function. So, like you can see on the example above, you have to access the variable trough the instance variable "this".
I knew about these two statements before (especially about "on replace" which was available before JavaFX 1.3), but I really discovered their power the last few days. It's a really powerful tool and is a good replacement for the good old passing-around-the-instance-variable.
I hope I was able to show a part of the power of "on invalidate" and "on replace".








How to use Quote function: