Sunday, December 07, 2008

Migrating from preview SDK to 1.0 of JavaFX

I'm developing a course that includes the JavaFX technology. That part was written a few months ago, based on the JavaFX preview SDK. As the JavaFX 1.0 has been released, I convert the programs from the JavaFX preview to 1.0. The following is a summary of the changes in JavaFX that I notice during the conversion.
  • To define constant values, use the def keyword (instead of the readonly keyword in the preview). E.g.,
    def PI = 3.14;
  • To write an application (with Swing frames), use the javafx.stage.Stage and the javafx.scene.Scene classes as follows. There is no javafx.ext.swing.SwingFrame class or javafx.application package.
    Stage {
      scene: Scene {
        content: ...
      }
    }
    Programs written in this way can also be executed as applets without modification.
  • Swing components in the javafx.ext.swing package are prefixed by Swing. For example, the Swing label is javafx.ext.swing.SwingLabel (instead of javafx.ext.swing.Label in the preview).
  • To layout GUI components, use the classes in the javafx.scene.layout package, such as the VBox and HBox classes. There is no panel class in the javafx.ext.swing package (such as FlowPanel in the preview).
  • The preferred size of a Swing component in javafx.ext.swing package cannot be modified when the it is created. (It could be in the preview.)
  • In class definition, attributes (instance variables) are declared using the var keyword (instead of attribute in the preview). E.g.,
    class Person {
      var name: String;
    }
  • To declare a function that overrides a superclass's function, use the override keyword (no need in the preview):
    class Person {
      override function toString() : String { "..." }
    }
  • There is no menu support. (The Menu and MenuItem classes in the javafx.ext.swing package exist in the preview but not in 1.0.)
  • The shape classes, such as Rectangle and Circle, are in the javafx.scene.shape package (instead of the javafx.scene.geometry package).
  • To start, or play, a Timeline, call the Timeline's play() method (instead of the start() method in the preview.)
  • Shapes, in the javafx.scene.shape package, are not transparent by default. Set a shape's color attribute to Color.TRANSPARENT to make it transparent.
  • The + operator cannot be applied to strings, e.g., 10 + " cents" is not valid. Use string interpolation instead. E.g.,
    int n = 10;
    var s = "{n} cents";
  • There are built-in functions print() and println(). There is no need to use java.lang.System.out.println() and java.lang.System.out.print() now.
  • There is a built-in function FX.exit(). There is no need to use java.lang.System.exit() now.

1 comment:

Kendrew said...

Just read another blog about the changes from preview to 1.0 of the JavaFX SDK: http://steveonjava.wordpress.com/2008/12/07/migrating-from-the-javafx-preview-release/