Wednesday, October 5, 2011

JavaOne 2011: JavaFX 2.0 with Alternative Languages

Stephen Chin and Dean Iverson presented "JavaFX 2.0 with Alternative Languages" to end the third of four technical JavaOne days. The presentation was held in Nikko Ballroom III and was another surprisingly lightly attended presentation. There were probably fewer than twenty in attendance and I suspect that other sessions at the same time may have had lighter than normal attendance as I heard several developers talking about going to dinner or other events after the last session time.

Chin briefly reviewed JavaFX 2.0 and talked about the ability of JavaFX 2.0 to "embrace all JVM languages." He also highlighted that "JavaFX Script is no longer supported by Oracle" though Visage is the "open source successor to the JavaFX Script language."

The presenters provided a slide with a "Hello World" style of JavaFX application that was a class definition with only the static "main" function defined.. They showed a slightly more complex example with the static "main" function and with an overridden version of the start(Stage) method. They then showed an example, still in Java, of using the Builder approach.

Binding (Chin called it "the biggest JavaFX Script innovation") has survived from JavaFX 1.3 to JavaFX 2.0. Changes in both directions are allowed and lazy invocation is supported for performance optimization.

Chin discussed Observable Pseudo-Properties that support "waiting for changes to properties" and there will be future closure support for this. The slides showing code for this were "animated" to show what each piece of syntax does. The code was "icky" due to use of anonymous classes because closures won't be available in Java until JDK 8 (summer 2013). Chin promised to show JavaFX making this easier already.

Groovy and JavaFX

Iverson believes that Groovy provides the easiest way to use JavaFX. He is one of the two people primarily behind GroovyFX. Iverson outlined some of the features and benefits of Groovy including the ease of porting from Java and the heavy use of Builders. Iverson's "Java vs. GroovyFX DSL" slide showed much more concise and more fluent syntax for the GroovyFX DSL example than for the straight Java example.

GroovyFX provides a SceneGraphBuilder. The advantages of Groovy's Builder style were obvious as Iverson showed the typical name/value pairing syntax for specifying attributes of a circle. The 'bind' keyword is used to explicitly find to properties.

Iverson showed a code sample "that is not hard, but is tedious" that demonstrated implementing properties in Java for a Person class. Iverson showed a GroovyFX annotation @FXBindable that makes all that tedium go away. I was really impressed with the ability to use Groovy closures in GroovyFX to handle the event listeners much more succinctly. Iverson showed tedious code necessary in Java to use the TableView ("a great control") and then showed how much simpler it was in GroovyFX. Iverson showed similar improvement in conciseness and expressiveness for layout when comparing GroovyFX to Java use of JavaFX. Iverson showed a slide that demonstrated several of the effects supported in GroovyFX and added that GroovyFX supports FXML. He stated that all basic controls are supported in JavaFX. Finally, Iverson stated that GroovyFX supports Griffon, which he described as similar to Grails for the desktop.

Clojure and JavaFX

Chin introduced Clojure and provided a brief background on this functional language. Because of its fewer side effects, it is optimized for high concurrency. Chin emphasized that its syntax "looks nothing like Java!" One of the slides, "Clojure Syntax in One Slide," compared Java syntax to Clojure syntax. Chin talked briefly about Clojure macros, but did not show any of them.

Iverson's examples using GroovyFX to integrate Groovy with JavaFX. Chin's code example for Clojure had no intermediary and was direct Clojure code for JavaFX interaction. The Clojure example employed doto to interact with Java. Chin said this could be a lot "cleaner and nicer" if someone who knows Clojure would build a little DSL or wrapper similar to what's available for Groovy and Scala.

Scala and JavaFX

Chin is a fan of Scala with JavaFX. He talked about advantages of Scala such as static type checking, closures, declarative characteristics, and support of type-safe DSLs (implicit conversions, operator overloading, and advanced language features). Chin talked about using implicits to extend another class (he used java.lang.String as an example) to add a method in a typesafe manner. Like Groovy, Scala supports operator overloading via specially named methods.

As was shown in the Groovy section of the presentation, the "Java vs. Scala DSL" slide showed significant improvement in conciseness and expressiveness between from Java to Scala. There was a feigned debate regarding whether Groovy/GroovyFX (fewer lines) or Scala/ScalaFX (fewer characters) was better, but the "agreement" was that either are easier to read and write than Java.

ScalaFX supports a bind operator. The Chin example showed this binding and also demonstrated use of an aggregation function (max()) along with conditional expressions and compound expressions. It is much more difficult in JavaFX 2.0 than it was in JavaFX 1.3 to properly bind to the entire path used in a bind expression, but ScalaFX makes this easier by wrapping the JavaFX 2.0 select API. Another ScalaFX example also showed use of Scala closures for "100% type-safe" event listener handling.

Other JVM Languages

Other JVM languages covered in a single slide ("Other JVM Languages to Try") were JRuby, Gosu, Mirah, and Fantom. Chin pointed out that, like Scala, Fantom runs on the CLR. Chin showed a slide with Fantom sample code that has been based on Fantom's Swing support. It is non-existent at this point, but ishow it would likely look. Fantom and Gosu were covered in an earlier presentation today.

Project Visage

Chin showed an example using Project Visage (JavaFX 1.3/JavaFX Script). He also talked about it being able to make Android native calls. He had a slide called "Visage is JavaFX Script++" and says that many things have been added to JavaFX Script. Visage supports default parameters, has new literal syntax, has a null check (.!) operator (null safe by default, opposite of Groovy), and has bindable maps coming soon as a built-in feature. Chin had a slide outlines other advantages of using Project Visage with JavaFX.

Conclusion

Chin and Iverson concluded by saying that JavaFX applications can be written in Java, but is also "usable in alternate languages." There is "improved support" using DSL libraries like GroovyFX and ScalaFX or via a dedicated UI JVM language such as Visage. The presenters covered significant territory in this presentation including JavaFX, Groovy/GroovyFX, Scala/ScalaFX, Clojure, Fantom, and Visage.