Java Reference
In-Depth Information
What this expression actually translates to is the following JavaFX code in Java:
rect1.heightProperty().bind(rect2.heightProperty().add(rect3.heightProperty()));
Even for a simple bind expression such as this, it is easy to get lost in all the parentheses and method calls that
Java requires.
You can also do aggregate operators in ScalaFX. Rather than having to use the static methods on the JavaFX
Bindings class, the ScalaFX Includes import gives you all these functions as methods you can call directly, such as the
following code to bind the width of one rectangle to the max width of three others.
rect1.width <== max(rect2.width, rect3.width, rect4.width)
The other type of bind expression that is extremely common is conditional statements. Creating conditionals
with the JavaFX APIs is possible, again by using the static Bindings class, but much simpler using the ScalaFX APIs.
The following code changes the strokeWidth based on whether the cursor is hovering (just as we did in the Vanishing
Circles application earlier).
strokeWidth <== when (hover) then 4 otherwise 0
The expressions passed in for the conditional value and result clauses can be arbitrarily complicated. The
following example combines Boolean logic, String concatenation, and a conditional expression.
text <== when (rect.hover || circle.hover && !disabled) then textField.text + " is enabled"
otherwise "disabled"
In all of these examples, because you are writing code that sits directly on top of the JavaFX bind APIs, you get all
the same benefits, such as lazy evaluation. Also, because Scala is a statically typed language like Java, you get these
benefits without giving up type safety as you do with other dynamic languages. In fact, you get better type safety with
the ScalaFX APIs, because it also supports type-safe dereferencing of subproperties. For example, in ScalaFX to bind
the width of a rectangle to the width of a Scene you would simply write:
rect1.width <== stage.scene.width
This works even if the Scene has not been created yet, such as during initialization of the Stage object. You can
also accomplish the same thing in Java, but it requires a non-type-safe property selector:
rect.widthProperty().bind(Bindings.selectDouble(stage, "scene.width"));
Underneath the covers, the ScalaFX calls this exact JavaFX API, but it protects the application developer from
accessing a property that might be misspelled or of the wrong type.
Finally, a discussion of binding would not be complete without an example of bidirectional binding. This is
similarly easy in ScalaFX, and can be accomplished using a slight variation on the bind operator as shown in the
following example.
textField.text <==> model.name
This creates a bidirectional binding between the name property in the model and a TextField , such that if the
user edits the text field, the model object will automatically be updated, too.
Search WWH ::




Custom Search