Java Reference
In-Depth Information
The Scala example makes use of an object literal pattern for constructing the scene graph, which gives the same
benefits as the Java and Groovy builders. It also benefits from the powerful binding support that is built into the
ScalaFX libraries. Some of the Scala language features that this code takes advantage of include the following:
Operator overloading : Scala lets you overload the existing operators and create entirely new
ones. This is used in several places, including the bind and animation syntax.
Implicits : Scala's answer to class extension is via the implicit conversions. This makes it
possible to extend the JavaFX API classes with convenience methods for object literal
construction, giving you all the power of builders with the core classes.
Closures : Scala also supports closures, which makes event handlers much more concise, and
allows powerful looping constructs such as the for . . . yield syntax.
DSL-friendly syntax : The usual code punctuation, such as dots and parentheses, is optional in
most situations, making it possible to build fluent APIs that read like language keywords.
Although the code ends up being fairly short and easy to understand, there is quite a bit of depth to the Scala
language. We show how you can take advantage of some of the built-in features of Scala to further improve your
applications in the section later in this chapter entitled “Scala and JavaFX.”
As you have seen, using a DSL to write your JavaFX code can produce significant benefits over what you would
have to write in Java using either the imperative or builder styles. Also, because all of these languages sit on top of the
same underlying JavaFX APIs, they have the same functionality as applications written in pure Java. Therefore, the
choice is yours as to which language you want to use to write your JavaFX applications.
In the next few sections we go into each of these languages in more detail to help you get started with developing
your JavaFX applications using them. Also, it is possible to use virtually any JVM language to write JavaFX applications
with, so if you have a favorite language that is not listed here, it is worth a try!
Making Your JavaFX Groovy
According to job trends, Groovy is the most popular language that runs on the JVM other than Java. 1 This is helped by
the fact that Groovy is extremely easy to get started with; other than a few minor differences, 2 any Java program is also
a valid Groovy application. This makes it very easy for Java developers to start using it, even before they appreciate all
the power and convenience that Groovy brings to the Java ecosystem.
Groovy source files compile directly to Java bytecodes and can run anywhere you have a JVM. As a result, you can
access any Java libraries directly, including the JavaFX APIs. A well-written Groovy application will almost always be
shorter than the equivalent Java version, because of all the conveniences built into the language. Some of the features
of the Groovy language that make it attractive as a replacement for Java and JavaFX code include the following:
Closures : Recently introduced in Java 8, closures are particularly important for GUI
programming, because they make it much simpler to write code that gets called when an
event happens.
Operator overloading : Groovy lets you overload existing operators or define new ones, which is
important for writing a readable DSL.
Dynamic typing : Types are optional in Groovy code, which makes it easier to write succinct code.
Getter/setter access : Groovy will automatically convert direct field access to getters and setters,
which shortens this very common pattern for accessing Java APIs.
1 Scala,Groovy,Clojure,Jython,JRuby,andJava:JobsbyLanguage, http://bloodredsun.com/2011/10/04/scala-groovy-
clojure-jython-jruby-java-jobs/ ,October2011.
2 DifferencesfromJava, http://groovy.codehaus.org/Differences+from+Java ,2011.
 
Search WWH ::




Custom Search