Archive for August 7th, 2008

To those Java developers learning JavaFX Script will be dismay to find that the useful interface and enum Java language constructs are not supported. Even though the ability to leverage and reuse existing Java myriad library is quoted as one of the greatest strength of JavaFX, JavaFX’s support for datatypes are currently limited to primitive data wrappers (Integer, Boolean, Number, String) and explicit class. Even though JavaFX Script may not explicitly support interface and enum , however all is not lost and developers can use workaround that involves some extra works. This solution is possible because JavaFX uses Class, the foundation of object-orientated programming, and that interface and enum are both closely related to class. In short, the interface construct allows constant declaration and provides method declaration without body. The enum provides a more powerful way of defining constant which is more elegant and less error-prone. Adapter Java class may include inner class and inner interface and still compile fine in JavaFX Script, however these will be ignored in JavaFX Script.

See  http://java.sun.com/developer/technicalArticles/scripting/javafx/javafx_and_java/

Recently, having browse through the source code in JavaFX, it evidently that interface and enum are indeed been used in JavaFX Script (though directly using interface, enum keywords).  It turns out that interface can be defined in the following manner. Note that user has to define all the interface methods even if one used a adapter class to provide dummy implementation for the interface. Interestingly, variable defined in interface and enum can be used in JavaFX Script like in Java class. Do take note that there is no switch statement in JavaFX Script.

var variable:Interface = Interface { body }

eg taken from Frame.jx in JavaFX Script Source Code

private attribute winListener: java.awt.event.WindowListener;

winListener = java.awt.event.WindowListener { // WindowAdapter has no effect. One still need to define all the interface method of WindowListener)
                                  public function windowClosing(e:WindowEvent):Void {
                                      if (disposeOnClose) {
                                           frame.dispose(); // fix me…
                                      if(onClose != null) {
                                      if (hideOnClose) {
                                          visible = false;
                                          showing = false;

… define rest of WindowListener’s methods



JavaFX Script can reuse Java’s class and methods through using following Adapter pattern but can Java uses function and class of JavaFX Script ?


JavaFX Script is still evolving but at this point the answer is NO (even though there might be just workaround) because syntax difference between JavaFX Script’s class and Java’s class. Function in JavaFX is a first-class object and there is also no concept of function in Java. So if JavaFX Script wants to pass data and object to Java’s class and method then pass it using standard primitive type wrapper and Java class.


The following table shows how to convert/wrap Java’s interface and enum into compatible implementation (class/method) to be used in JavaFX Script.



Read Full Post »