Static Fields and Functions in JavaFX

26 03 2010

A static field or method is one that is accessible via the class itself without the need for an instance of the class. Constants, such as the number of months in a year, or methods that perform calculations that don’t require access to any instance fields are examples of cases where static fields and methods should be used. Java has had static fields and methods since its inception. JavaFX used to have static fields and functions, but no longer does. They have been replaced with script level variables and script level functions. A script level variable or function is declared outside of the class definition and behaves pretty much like static fields and methods in Java.

Here is an example of static fields and methods in Java in a file called TimeTools.java:

public class TimeTools {

    public final static int MONTHS_IN_YEAR = 12;

    public static int yearsToMonths(int years) {
        return years * MONTHS_IN_YEAR;
    }

    public static void main(String[] args) {
        int years  = 5;
        System.out.println("Since there are "
          + TimeTools.MONTHS_IN_YEAR
          + " months in a year, there are "
          + TimeTools.yearsToMonths(years)
          + " months in " + years + " years.");
    }
}

In this case the TimeTools prefix wasn’t needed because the main method was in the TimeTools class, but it would be needed if the field or method were being accessed from a method in another class.

And here’s the equivalent in JavaFX Script in a file named TimeTools.fx:

public class TimeTools {
}

public def MONTHS_IN_YEAR = 12;

public function yearsToMonths(years:Integer):Integer {
    return years * MONTHS_IN_YEAR;
}

function run() {
    var years  = 5;
    var months = TimeTools.yearsToMonths(years);
    println("Since there are {TimeTools.MONTHS_IN_YEAR} months in "
            "a year, there are {months} months in {years} years.");
}

Notice that MONTHS_IN_YEAR and the yearsToMonths() function are both declared outside of the TimeTools class definition. If they were inside the class then an instance of TimeTools would be required in any place where they were going to be accessed.

Again, the TimeTools prefix is only needed from outside of this file.

So while there’s no longer a static keyword in JavaFX, the equivalent functionality can be achieved through script level variables and functions. Just remember to pull your fields and functions outside of the class definition and then use them like you did in Java.

Joshua Smith





Animated JavaFX Candle

28 02 2009

Sang Shin and Jim Weaver are teaching a free 15 week online class entitled JavaFX Programming (with Passion!). For the section 4 homework students are asked to make a JavaFX candle. I decided to give it a try and this is what I came up with. In the applet version the flame is animated.


JavaFX Candle

Joshua Smith





TS-5138 JavaFX – Sexy Interfaces for Mere Mortals

28 02 2009

Below is an article by Richard Marejka on my JavaOne presentation.

2008 JavaOne Conference: Java FX Platform – Sexy Interfaces for Mere Mortals

The actual presentation is available as multimedia, PDF and MP3 with a free Sun Developer Network (SDN) login.

Joshua Smith





JavaFX: Sexy Interfaces For Mere Mortals

28 04 2008

I will be speaking at JavaOne in San Francisco on Tuesday, May 6, 2008 from 12:10pm – 1:10pm. Below is the abstract for my session.

Joshua Smith

TS-5138 – The JavaFX™ Platform: Sexy Interfaces For Mere Mortals

JavaFX™ Script is a compiled, declarative scripting language that runs on the desktop, in the web browser and on mobile devices. It’s tightly couple with Java™ technology for core application functionality, heavy lifting and libraries. It makes special effects and GUI transitions accessible to people who aren’t graphic artists or know the intricacies of overriding Swing painters and working with the Java 2D™ API affine transformations. JavaFX Script is a perfect user-interface compliment for Java applications.

Throughout the session we’ll look at some powerful language idioms that make special effects, interface transitions and vector graphics accessible to programmers who have not chosen to specialize in advanced Swing and Java 2D. The bind keyword connects model and view and handles two-way propagation of changes. Sequences are a data structure similar to arrays, but allow for things like “all numbers between 1 and 100 that are divisible by 3”. Key-Frame animation is used to “animate” the value of variables over time. A very dynamic user interface is possible when you bind things like position, opacity and radius to these values. Triggers allow functionality that is similar to property change listeners. Transform and morphing features make movement, scaling and shape changes possible in one or two lines of code.

The majority of the session will be spent looking at visually rich examples of what is possible in Java FX Script. We’ll look at examples of image transitions, fade-ins, fade-outs, morphing and a variety of effects. Full source code of all examples will be provided. Come expecting to see lots of code. The techniques demonstrated will be directly applicable to both consumer and business applications.

No prior knowledge of JavaFX Script is required. It is expected that the attendee will have a working knowledge of Java as there will be many comparisons made between how things are done in Java vs. how they are done in JavaFX Script. Basic knowledge of Swing is helpful, but by no means necessary.

JavaOne Speaker





DevJFX Opens Its Doors

27 04 2008

Java FX Script is a compiled, declarative scripting language that runs on the desktop, in the web browser and on mobile devices. It’s tightly couple with Java for core application functionality, heavy lifting and libraries. It makes special effects and GUI transitions accessible to people who aren’t graphic artists or know the intricacies of overriding Swing painters and working with Java 2D affine transformations. Java FX Script is a perfect user-interface compliment for Java applications.

This blog is dedicated to Java FX Development.

-Joshua Smith