mardi 19 juin 2012

Just bind it

The javafx binding concept is nice and powerful. But sometimes it might be a bit difficult to figure out how to use it.
Let’s assume you have the following bean
and you need a variable bound to the sum of x*y for a sequence of those beans. Actually this problem can be expressed more generally: how to bind a variable to the result of a function applied to a sequence of elements (i.e. have it updated each time the sequence is modified or a field of a bean is modified). In java you would required tons of PropertyChangeListener registered correctly. With javafx you can write it in a more concise way using the bind keyword andbounded functions. But the first time I wanted to write it, I had to fight a little bit with the syntax. So I wanted to share this small unit test demonstrating a way to achieve this:

Aucun commentaire:

Enregistrer un commentaire