Java Reference
In-Depth Information
It also has a convenient shortcut syntax for converting all the variables in a class to properties:
@FXBindable
class Person {
String name;
int age;
String gender;
Date dob;
}
GroovyFX Binding
Binding in JavaFX is an extremely powerful feature, but the API-based syntax introduced in JavaFX 2 can often get
in the way of understanding what the bind code is doing. GroovyFX solves this problem by taking advantage of the
operator overloading feature of the Groovy language to provide an infix notation for common bind expressions.
For example, to bind one rectangle's width to the width of a second rectangle, you can write the following
code in GroovyFX:
rect1.widthProperty.bind(rect2.widthProperty)
There is also an alternate version of the same code that you can use instead:
rect1.widthProperty.bind(rect2, 'width')
However, the real power of GroovyFX binding comes into play when you are combining multiple properties in a
bind statement. As a second example, let's say that you want to bind one rectangle's width to the sum of two others.
In GroovyFX you can write the following code:
rect1.widthProperty.bind(rect2.widthProperty + rect3.widthProperty)
This would translate to the following, much longer, JavaFX Java code:
rect1.getWidthProperty().bind(rect2.getWidthProperty().add(rect3.getWidthProperty()));
The GroovyFX distribution comes with an example of some binding code to animate the hands of an analog
clock. This example was written by Jim Clark, and the relevant code showing properties and binding is shown in
Listing 13-7.
Listing 13-7. Analog Clock Excerpt from the GroovyFX Demo Package
@FXBindable
class Time {
Integer hours
Integer minutes
Integer seconds
Double hourAngle
Double minuteAngle
Double secondAngle
 
Search WWH ::




Custom Search