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.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. 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, SimpleStringProperty is one: a Label exposes its text through textProperty(), which can be observed or bound instead of calling setText.

  • 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());
    `

    • Alan
    • Ada
    • null
    • Nothing: an exception is thrown on line 4
    Show the answer

    Alan

    After bind, copie takes nom's value at every change. Writing into nom, the source, is allowed; writing into copie is 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; with bindBidirectional, 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 nom with bind, but it must show "Inconnu" while nom is empty. Which fix keeps the label always in sync with nom?

    • Bind the label to Bindings.when(nom.isEmpty()).then("Inconnu").otherwise(nom)
    • Call unbind() before each setText("Inconnu") when nom is empty
    • Wrap the setText in a try/catch to ignore the exception
    • Replace bind with bindBidirectional so 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; the try/catch writes nothing; bindBidirectional would send "Inconnu" back into nom.