ZiltiTech

Desktop and server programming with the JVM

JavaFX: "on invalidate" and "on replace"

Today (after a loooong break) I want to write about two great features in JavaFX:
"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".

Using the deployJava.js Deployment Kit instead of dtfx.jsVision & Project: Lopia - Merging social networks, across all devices

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies