JavaFX and Java Interoperability

Posted: December 26th, 2008 under JavaFX Technology.
Tags: , , , ,

Bookmark and Share

[ 2009/06/22 Update: My latest article discussed how to use
Pure Java Code to Call JavaFX Class]



From the JavaFX blog, an article discussed the possiblity of invoking methods of JavaFX classes from Java. JavaFX can call Java code without any problem, however, the reverse is not supported by JavaFX. Doing some googling shows that programmers are trying all kinds of hacks to invoke a JavaFX class method from Java. You can check out an interesting article on reverse engineering of JavaFX classes. Even the example on JavaFX blog provided a hack to work around this.

 

So do we have the need of such kind of interaction between Java and JavaFX? I say it is a “YES”. If Java and JavaFX can be used interchangeably(when possible), this could give more life to JavaFX in the long run. Just consider the MVC design pattern, we can write an application by using Java and JavaFX together. The “M” and “C” part can be implemented in Java while the “V” can be done by JavaFX. It would be very interesting to see this.

 

Right now, there are a few “standard” ways to call JavaFX from Java:

1) Using the ScriptEngineManager class. From Geertjan Wielenga’s article, we can do it in this way:

package calc; 

import java.io.InputStreamReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException; 

public class CalculatorLauncher { 

public static void main(String[] args) {
 try {
 ScriptEngineManager manager=new ScriptEngineManager();
 ScriptEngine engine = manager.getEngineByExtension("fx");
 InputStreamReader reader = new InputStreamReader
 (CalculatorLauncher.class.getResourceAsStream
 ("Calculator.fx"));
 engine.eval(reader);
   } catch (ScriptException ex) {
  }
 }
}

However, this is just like System.exec(”calc”) as pointed out by cronseaux. I agree with him. A even simpler way is to use System.exec(”javafx Calculator.fx”) to complete the above code. So this is not a good solution.

 

2) Manage to use java reflection to call JavaFX class methods. This one should work because JavaFX classes are compiled into Java classes and byte code. However, the complexity makes it almost unusable and code written in this way has no readability.

 

3) A third approach is to define an interface in Java and implement it in JavaFX. For example,

public interface JavaInterface
{ ... }

In MyJavaFXClass.fx, do something like:

public class MyJavaFXClass extends JavaInterface
{ ... }

In you java code, just invoke the JavaFX object directly using the interface. This approach can solve most of the interoperation issues. Just that an interface is needed for every JavaFX class which is going to be called from Java. Though it is very cumbersome, at least it is the best workaround I can find so far.

 

Since this is the first release of JavaFX, I don’t criticize much on this given the strong powerful features of JavaFX. I do hope the future releases of JavaFX can improve on this.

[ 2009/06/22 Update: Please refer to my latest article for discussion on: Pure Java Code to Call JavaFX Class ]

Bookmark and Share

2 Comments »

  1. [...] 在JavaFX 1.0发布之后,本人撰写的文章JavaFX和Java之间的互操作性被各网站转载。文中总结了3种从Java调用JavaFX的方法。这三种方法分别为: [...]

    Pingback by 用纯Java代码调用JavaFX的功能 — June 21, 2009 @ 2:16 am

  2. actually i still don’t understand about the difference between java and javafx..
    please give me more explaination..
    thanks

    Hi Riza,

    JavaFX is a client side scripting language which is based on Java technology. JavaFx can call Java classes directly, but not vice versa. You can check out the tutorials on javafx.com to find out more on this.

    Rgds,

    Henry

    Comment by riza — July 25, 2009 @ 8:55 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment