http://java.sun.com/javaone http://java.sun.com/javaone http://java.sun.com/javaone
JavaOne - Experiencing Java technology through education, industry, and community
  2009 Platinum Cosponsor
Intel
  Cosponsors
Cosponsors
  Gen Session Cosponsors
General Session Cosponsors
  Media Sponsors
Media Cosponsors
Home > Technical Session: Introduction to JavaFX Script

Technical Session: Introduction to JavaFX Script


One of the most-populated sessions of the first day of the 2009 JavaOne conference was Introduction to the JavaFX Programming Language (TS-5576), by Stuart Marks, a developer at Sun Microsystems. The session was a whirlwind of important information that no novice JavaFX programmer should be without.

Marks started off by describing JavaFX as a "scripting language for interactive graphics, media, and animation." He pointed out some of JavaFX technology's key features:

  • JavaFX is object-oriented.
  • It is an "expression language."
  • It is both declarative and procedural.
  • It integrates with Java technology.
  • It incorporates the concept of time.

JavaFX has many scriptlike features, as well as the convenience of data binding. It also allows programmers to create functions both inside and outside of classes, function variables, and even anonymous functions. In addition, it has rich object functionality, including the ability to inherit both classes and a new type of pseudoclass in JavaFX 1.2 called a mixin, which is similar to interfaces in Java technology.

Marks then mentioned that JavaFX technology has "sort of" a scripty feel to it. A JavaFX script is essentially a sequence of expressions. In other words, everything inside JavaFX -- including loops, conditionals, and even blocks -- is, in fact, an expression that results in some value, even if the value is simply void.

In this session, attendees also learned that in the JavaFX environment, all variables must be declared, either with a var keyword, which indicates that the variable can change value, or a def keyword, which indicates that the variable cannot be reset other than by using the bind keyword. But JavaFX can use type inference to determine what data type a variable is, if possible.

However, while JavaFX can use type inference, Marks pointed out that JavaFX is statically typed. In other words, the language figures out what type a variable is at compile time, never at runtime.

Marks then introduced the base JavaFX types: Boolean, Number, Integer, and String. Strings are similar to their Java counterpart, although in JavaFX, you cannot have a String reference that is null -- JavaFX does not throw NullPointerExceptions the way the Java programming language does. A null string reference is simply mapped to an empty String.

Another useful bit of information: You can also place full expressions inside the curly braces, where the expression will resolve to the appropriate value to insert into the String. Finally, JavaFX also has the Duration data type, which is used to express a quantity of time -- it is frequently used with animations.

In JavaFX, functions are declared using the function keyword, are scoped using curly braces, and may consist of zero or more parameters stated inside parentheses. In addition, functions may contain a return value, which, unlike Java, is declared after the parameters and separated by a colon. For example, a function in JavaFX looks like this:

function add(argOne: Integer, argTwo: Integer) {
     result = argOne + argTwo;
     println("{argOne} + {argTwo} = {result}");
}

The session also introduced JavaFX classes. Classes are declared using the class keyword followed by the name of the class. Fields and member functions are then declared inside the curly braces. As in the Java programming language, you can use the extends keyword for class inheritance. Unlike in the Java language, however, if you wish to override a method in a superclass, you must explicitly specify the override keyword before the class declaration. Here is what a class looks like in JavaFX:

class Customer {
     var firstName: String;
     var lastName: String;
     var phoneNum: String;
     var address: Address;

    function printName() {
        println("Name: {firstName} {lastName}");
    }

    function printPhoneNum(){
        println("Phone: {phoneNum}");
    }

    function printAddress(){
        println("Street: {address.street}");
        println("City: {address.city}");
        println("State: {address.state}");
        println("Zip: {address.zip}");
    }
}

Marks then spent some time on sequences. Sequences are analogous to arrays in the Java programming language, but with additional functionality that makes the data easier to manage. For example, you can abbreviate a known range in a sequence by using the first entry, followed by two dots and the final entry. You can determine the size of a sequence by using the sizeof operator. You can also add values into sequences either at the end of, before, or after a specific indexed value, using the insert keyword. In addition, you can remove values or indices from a sequence using the delete keyword.

Much as in the Java programming language, you can use the for/in statement to iterate through each item in a sequence, assigning the current value to a local variable for each iteration of the loop. The difference here is that whereas Java uses a colon between the local variable and the array, JavaFX uses the in keyword between the local variable and the sequence.

Binding is one of the most valuable features of JavaFX technology. Bound variables work by mapping any changes to one variable immediately to another, so that the two always remain in sync. However, Marks pointed out that bind performs "minimal recalculations," meaning that JavaFX will recalculate only those portions of a bind that it needs to get the job done. Variables are not the only things that can be bound in JavaFX. You can also use the bind keyword before an object literal instantiation to tell JavaFX that each time a value inside that object changes, the variable that represents it should be updated to point to a new object with the current and updated fields.

A replace trigger, which Marks mentioned as "sort of the opposite of data binding," can be set to execute code whenever a variable changes its value. In JavaFX, the keywords on replace are used to set up the replace trigger, followed by a variable name that represents the previous value of the variable.

Finally, the package modifier can be used on a class, variable, or function to widen its access. The public modifier works the same as in the Java programming language: It indicates that a class, variable, or function can be accessed from anywhere. The protected modifier, however, restricts access to either subclasses or to other code within the same package. The default access modifier in Java is package private, whereas the default access modifier in JavaFX is private.

The public-read and public-init modifiers are new in JavaFX technology and can be used only with variables. The public-read modifier says that a variable can be read from anywhere, but it can only be reset from within the same script. Likewise, public-init specifies that a variable can be both read and initialized from anywhere but can only be reset from within the same script. As with public-read, you can also prepend package or protected to the keyword to widen the scope of who can reset the variable value.

As such, programming in JavaFX is similar to programming in Java technology, but implementing classes is a little different. For example:

  • There are no getters. Instead, use public-read variables.
  • There are no setters. Instead, use public variables with a trigger.
  • There are no class constructors. Instead, use public-init variables and object literals.
  • There are fewer listeners and callbacks. Instead, you should expose state variables and allow binding on them.

JavaFX is a wonderful new language for programming rich clients, and this session showed some of the best that JavaFX has to offer. For more information, including examples of each of these features, see the official JavaFX home page.

 

Do you have comments about this article? We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies - your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.