Java Reference
In-Depth Information
Table 13-1. Property Operations in ScalaFX Versus Java
Operation
Java
ScalaFX
Description
getFooProperty()
foo
Get Property
Get the raw property (typically used
for setting up binds or listeners)
1: getFoo()
2: getFooProperty.get()
[long form]
1: foo()
2: foo.get() [long form]
Get Property Value
Get the value of the property
1: setFoo(val)
2: getFooProperty.set(val)
[long form]
1: foo() = val
2: foo.set(val) [long
form]
3: obj.foo = val [object
literal shortcut]
Set Property Value
Set the value of the property
Once you get used to the ScalaFX syntax, it is quite natural. In general, if you refer to a property directly, you
will be accessing the full property object, which has all the methods for binding, listeners, and so on. However, if you
follow the property name with parentheses, you will get (or set) the property value.
this use of parentheses is supported via the scala apply and update syntax, which is commonly used for Array s
and List s, but in scalaFX is used to differentiate between the raw properties and their values.
Tip
One exception to this rule is in the object literal case (third example of Set Property Value), where you can use
the assignment operator directly to set the value of a property (no parentheses needed). An unambiguous case, this is
allowed because JavaFX properties references are read-only, and it significantly cleans up the syntax of the user code.
If you ever get confused between properties and values, Scala's type system will come to the rescue. In general,
the ScalaFX APIs have been designed to preserve strong typing and produce type errors anywhere the developer's
intent is ambiguous. Therefore, if you use a property where a type is expected, or vice versa, the compiler will most
likely catch the error before you even run the application.
ScalaFX Bind APIs
Binding is arguably one of the most innovative features in the JavaFX library; however, the APIs can be a bit
cumbersome to use, especially in cases where you have complicated bind logic. The fluent API provided is quite
powerful and expressive given the constraints of the Java language, but lacks a lot of the elegance that made bind so
powerful with JavaFX Script.
The ScalaFX Bind APIs sit on top of the JavaFX binding support, but wrap them in a programming language
syntax that is natural to write and understand. By taking advantage of operator overloading and infix notation, you can
write complex ScalaFX bind expressions without even knowing all of the bind API methods that come with JavaFX.
For example, here is how you bind the height of one Rectangle to the sum of the heights of two others:
rect1.height <== rect2.height + rect3.height
Other than the special bind operator (<==), and lack of parentheses to convert from properties to values, this is
the same code that you would write to add up the heights statically. However, once the bind is in place, any updates to
rect2 or rect3 will dynamically change the height of rect1 .
 
 
Search WWH ::




Custom Search