Wednesday, June 27, 2007

JavaFX

So JavaFX is my new favorite programming language. Thats right - JavaFX is a new programming language.

I think a lot of people might be getting the wrong idea about JavaFX because of the marketing hype. When I first heard about JavaFX, it was positioned as an alternative to Flash (or Flex). As a way to create (and deliver) Rich Internet Applications. But upon further inspection one realizes that, at its core, JavaFX is really a new language that runs on the JVM. I would say that comparing JavaFX to Groovy (or even Spring) would be more appropriate than comparing it to Flash.

[Correction: Technically speaking JavaFX is a brand - JavaFX Script is the name of the programming language.]

I'm low on time right now and can't go into many details, but let me say this: JavaFX is not just another language that runs on the JVM, its a very cool language. By cool, I mean that JavaFX has a number of innovative features that produce immediate and pronounced payoffs. For example:

1. Type-safe JSON
Any one who has used languages like Ruby or Groovy or even JavaScript knows that those languages don't need all those damn XML files that are part and parcel to modern Java frameworks.

Essentially, any language that has syntax for array literals PLUS map literals has built-in support for declarative, XML-ish constructs:

For example in JavaScript:

var company = {
name: "Smart Soft",
url: "www.smart-soft.com",
people:[
{firstName:"Dave",lastName: "Ford"},
{firstName:"Michelle",lastName: "Goldstein"}]
}

Now JavaFX has this same feature - but with one distinct advantage: the map literals are not maps - they are typed Java objects. Here is how this would look in JavaFX:

var company = Company{
name: "Smart Soft"
url: "www.smart-soft.com"
people:[
Person{
firstName:"Dave"
lastName: "Ford"
},
Person{
firstName:"Michelle"
lastName: "Goldstein"}
]
}

This "typed" feature means that the IDE can give you auto-completion and find your errors sooner rather than later. Bottom line: integrated declarative-style programming + type-safe IDE auto-completion and early error detection = big productivity gains.

2. Language level support for bi-directional data binding. This is analogous to JSF's "value binding expressions" or Microsoft's "bound controls". The data binding feature supports MVC architectures in the most elegant manner I have seen yet. You simply declare that a particular UI text field is bound to some field in your model object. And then JavaFX keeps the model and view in synch automatically.

There is much more to JavaFX than I have mentioned, but this should give a you a basic feel.

Minimal JavaFX Demo

Here are the absolute simplest steps to get a demo of JavaFX up an running:
  1. Download the 3 JavaFX jar files:

    javafxrt.jar
    Filters.jar
    swing-layout.jar


    You can get them from here:

    https://openjfx.dev.java.net/source/browse/openjfx/trunk/lib/

  2. Create a text file called Hello.fx:
    import javafx.ui.*;
    
    Frame {
        content: Label {
            text: "Hello JavaFX"
        }
        visible: true
    }

  3. Add the 3 jars and Hello.fx to your classpath

  4. Run the script like so:

    java net.java.javafx.FXShell Hello

    Note: The name Hello is resolved via the classpath (not the file system).

    Here is how my folders looked:


    ~/DavesJavafxDemo/classes/Hello.fx
    ~/DavesJavafxDemo/lib/javafxrt.jar
    ~/DavesJavafxDemo/lib/Filters.jar
    ~/DavesJavafxDemo/lib/swing-layout.jar


    Thus, my classpath must include the classes folder plus the 3 jar files:


    ~/DavesJavafxDemo/classes
    ~/DavesJavafxDemo/lib/javafxrt.jar
    ~/DavesJavafxDemo/lib/Filters.jar
    ~/DavesJavafxDemo/lib/swing-layout.jar


    Here is the shell script I used:

    cp=./lib/swing-layout.jar:./lib/Filter.jar:./lib/javafxrt.jar:./classes
    java -classpath $cp net.java.javafx.FXShell Hello

    Note: The way I have the paths specified, you must run this script from within DavesJavafxDemo folder.

    On Windows, the shell script (runDemo.bat) would look like this:

    set cp=.\lib\swing-layout.jar;.\lib\Filter.jar;.\lib\javafxrt.jar;.\classes
    java -classpath %cp% net.java.javafx.FXShell Hello