Migrating from the JavaFX Preview Release

7 12 2008

Now that the JavaFX 1.0 Release is out, it is time to dust off all your old JavaFX code and fire it up, right?  Well, not quite…  There were a whole lot of changes in the span of a few months, and it is guaranteed that your program will need a little TLC before it runs.  Fortunately, I’ve already gone through this pain on a relatively large codebase (WidgetFX), and can share what I learned.

Language Changes

  • Change “attribute” to “var”/”def” – There is no more attribute keyword, but it is a simple search and replace to change it to var everywhere.  On a second pass you might want to convert vars that are never changed to def for efficiency.
  • No More “static” – If you want to make a variable or function as static, just move it outside your class declaration.  If it is declared public you can access it from anywhere else just by prefacing it with the file name, no static modifier needed.
  • New Access Modifiers – The default access modifier was changed to private rather than package, so you need to change any attributes or functions that had no modifier to package and then can delete all references to private.  On a second pass it may be a good idea to button-up permissions by using the new public-init and public-read permissions.  Here is a quick-reference table:
    Preview 1.0 Migration
    private default Remove this keyword everywhere
    default package Add package anywhere no modifier was specified
    protected protected No change
    public public No change
    N/A public-init Use this anywhere the user should be able to initialize and read, but not update
    N/A public-read Use this anywhere the user should be able to read, but not initialize or update
  • Required “override” – It is now mandatory that you use the override keyword on any member variable or function that exists in the superclass.  Expect to be going through lots of compiler errors to find all the places you need to fix this…
  • {} Instead of “+” for Concatenating Strings – While the java-esque “+” operator for concatenating strings was an easy habit to fall into, it is no longer valid.  Instead you have to use the {} syntax to build strings.  For example, rather than “hungry for: ” + meat, you would write “hungry for: {meat}”.
  • Keyframe Syntax – While this is not required to get your code working, there is a new KeyFrame syntax for doing animation that has the potential to make your code much shorter.  Rather than creating an instance of the KeyFrame class you can simply write “at (duration) {x => y}”.

Read the rest of this entry »

Advertisements