Java Reference
In-Depth Information
Now you are finally ready to get started with some coding. The HelloGroovyFX application is very short, so you
can grab the code from the source bundle that comes with the topic, or just type in the code shown in Listing 13-4
yourself.
Listing 13-4. Hello GroovyFX Code
import groovyx.javafx.GroovyFX
import groovyx.javafx.SceneGraphBuilder
GroovyFX.start {
new SceneGraphBuilder().stage(visible: true) {
scene {
stackPane {
text("Hello GroovyFX")
}
}
}
}
To run the application, simply right-click the class and choose Run “HelloGroovyFX” from the context menu.
This gives you the application shown in Figure 13-6 .
Figure 13-6. Output from running the Hello GroovyFX application in IntelliJ
Congratulations! You have created and run your very first JavaFX application in Groovy. In the next few sections
we go into more detail on the features and capabilities of GroovyFX, but remember that anything you can do in JavaFX
is possible in GroovyFX, because it wraps the full JavaFX APIs.
Properties in GroovyFX
One of the features that would most benefit UI development in Java with JavaFX is having a notion of first-class,
observable properties in the language. Because this does not exist today, the JavaFX team added properties at an API
level, which is sufficient, but much more verbose than a native syntax can provide.
Fortunately, with dynamic languages such as Groovy, it is quite easy to add in powerful features including a
native property syntax. Groovy already has a built-in notion of simplified getter/setter access, so you can retrieve and
store JavaFX properties just as if they were normal variables. For example, to set the width of a Rectangle in Groovy,
all you need to write is:
rectangle.width = 500
This will be automatically translated to a setter call, such as the following:
rectangle.setWidth(500);
 
Search WWH ::




Custom Search