Data binding
Observable property, one-way and two-way binding, computed binding, converter, observable list, and the ban on writing into a bound property, using JavaFX properties.
- 25 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Observable property: what is it?
Show the answer
A value held in an object that tells its listeners whenever it changes, and that can be bound to another one.
In JavaFX,
SimpleStringPropertyis one: aLabelexposes its text throughtextProperty(), which can be observed or bound instead of callingsetText.What does this program print?
```java
StringProperty nom = new SimpleStringProperty("Ada");
StringProperty copie = new SimpleStringProperty();
copie.bind(nom);
nom.set("Alan");
System.out.println(copie.get());`AlanAdanull- Nothing: an exception is thrown on line 4
Show the answer
AlanAfter
bind,copietakesnom's value at every change. Writing intonom, the source, is allowed; writing intocopieis what would be forbidden.A property is bound to another in one direction with the … method, and in both directions with ….
Show the answer
A property is bound to another in one direction with the bind method, and in both directions with bindBidirectional.
Choosing the direction means choosing who may write: with
bind, only the source; withbindBidirectional, both sides.One-way binding: what is it?
Show the answer
A link by which a target property always takes its source's value, without the reverse change being possible.
target.bind(source): changing the source updates the target, but writing into the target throws an exception as long as the binding exists.The label is bound to
nomwithbind, but it must show "Inconnu" whilenomis empty. Which fix keeps the label always in sync withnom?- Bind the label to
Bindings.when(nom.isEmpty()).then("Inconnu").otherwise(nom) - Call
unbind()before eachsetText("Inconnu")whennomis empty - Wrap the
setTextin atry/catchto ignore the exception - Replace
bindwithbindBidirectionalso the label can be written to directly
Show the answer
Bind the label to
Bindings.when(nom.isEmpty()).then("Inconnu").otherwise(nom)The rule "empty gives Inconnu" is expressed in the binding itself, which recomputes by itself.
unbind()breaks the sync; thetry/catchwrites nothing;bindBidirectionalwould send "Inconnu" back intonom.- Bind the label to