Java Reference
In-Depth Information
Scala and JavaFX
Scala is a powerful JVM language combining the best features of functional and object-oriented programming in
a single language. As do the other JVM languages discussed in this chapter, it compiles source files directly to Java
bytecodes and all the existing Java language libraries commonly used are compatible with it. However, Scala adds
powerful, type-safe collections, an elegant actor model for concurrency, and functional language features including
closures, pattern matching, and currying.
Scala was started by Martin Odersky in 2001 at the École Polytechnique Fédérale de Lausanne (EPFL), and has
grown in maturity and popularity over the years. Odersky has actually been the genius behind the scenes working on
Java compilers for many years, including the Pizza language that extended Java and GJ, which became the grandfather
of the modern Java compiler after its adoption by Sun in Java 1.3. By developing an entirely new language on the JVM,
Odersky was able to overcome several of the inherent design limitations of Java.
Scala is used today in many large enterprises such as Twitter, LinkedIn, Foursquare, and Morgan Stanley. There
is also commercial support available from the creators of the language via TypeSafe, a Scala language company. Also,
Scala has been hailed as the successor to Java by James Gosling, the father of Java; James Strachan, creator of Groovy;
and Charles Nutter, JRuby Core Developer, among others. With all this support behind the Scala language, it makes a
great candidate for also providing superior APIs for JavaFX development!
Although you can code in Scala directly against the JavaFX APIs, the end result will look very similar to the Java
code we have been writing up to this point and will be unable to take full advantage of the language. The ScalaFX
project was started to provide a more Scala-friendly API for doing JavaFX development and is what we use in all the
examples throughout this topic.
ScalaFX is an open source project created by Stephen Chin, one of the authors of this topic, and has numerous
additional contributors who have helped with the design and testing of the library. It is very similar to the GroovyFX
library described earlier in this chapter, because it is also an open source library that constitutes a bridge between
a JVM language and the JavaFX APIs. However, ScalaFX is different in that it prioritizes type safety and consistent
semantics, which is in keeping with the spirit of the design goals of the Scala language.
Many of the constructs in the ScalaFX library were inspired by the JavaFX Script language that was used in
JavaFX releases prior to 2, so for those of you familiar with JavaFX Script, the syntax will feel quite comfortable. It
takes advantage of many of the advanced features available in Scala, but does not expose or burden the end user with
understanding these to build great-looking UI applications.
Making you an expert Scala developer is beyond the scope of this topic, but we do describe the Scala features as
we use them in our ScalaFX code, so this should also serve as a gentle introduction to Scala for anyone who is already
a proficient Java developer.
Getting Started with ScalaFX
To write your first ScalaFX application you need to download and install Scala as well as the ScalaFX library Because
ScalaFX code is a DSL written in the Scala language, you can use any IDE that supports Scala development, such as
IntelliJ, Eclipse, or NetBeans, although you might want to start with the Scala IDE for Eclipse because that is the one
supported by the Scala language team at TypeSafe. We demonstrate the basic setup of an Eclipse environment for
ScalaFX in this chapter, although the concepts apply to other IDEs as well.
To start, install the latest version of Eclipse and launch it. From the Help menu choose Install New Software . . .
and paste the Scala IDE update URL into the Work with field. You can get the latest updated URL for Scala IDE from
their web site at http://scala-ide.org/download/current.html .
This lets you select the Scala IDE for the Eclipse plug-in as shown in Figure 13-10 .
 
Search WWH ::




Custom Search