Interoperability between JavaFX and Java

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

From the official 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 really have the need of such kind of interaction between Java and JavaFX? I’d 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 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
   } 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.

[Update: Please refer to my latest article for discussion on

Pure Java Code to Call JavaFX Class]

Diecast Cars NASCAR Collectables

Online US Citizenship Practice Test

Jim Weaver’s feedback

Today, I received JavaFX guru Jim Weaver’s feedback on my demo game PACMAN. He even had a post about my game on his blog. I am so encouraged by him. Thanks a lot, Jim.

Many people visited my blog and played the game. I am really happy about it. Thanks for everyone’s playing and feedback.

Some people were asking me for the source code. I am working on the code towards a more complete version. After that, I will publish the code and write some documents on it. Hopefully I can finish it within a week(if I am not too busy at work… ^_^)

Layoff number

News came today from the government that 533,000 jobs were cut in November. Forbe’s statistics also indicated that 31,387 job losses in December so far were from top 500 public companies. The unemployment rate jumped to almost 7%. In total, more than 10 million people were unemployed as of November, while the number of employed was roughly 144 million.

Employers are slashing jobs to cut costs and cope with the contracting economy. Many of my friends are worrying about the security of their jobs, while frustrated by tanking stock prices and home values. This in turns results in more shrinking in consumer spending. And the cycle of even more job losses.

The most important thing for now is to get enough cash to at hand to survive this winter. Investment should be really conservative for most. Refer to Safest Investment .

JavaFX 1.0 Launched

Today, the JavaFX 1.0 SDK is officially released. It is really exciting! The JavaFX platform opens a huge market for developers and UI designers to work together for a rich interface application(RIA). Because Java technology is already on more than 10M devices(PDA, computers, cellphones, etc), this new scripting language is expected to be widely accepted by most Java developers.

One of the most innovative features, I think, should be the eye-opening Drag-to-install trick. This is support by the JDK 1.6U10. Applets can be dragged directly onto a desktop and run in a seperate window. This bring us the simplicity of installation of internet application.

Other useful and powerful features attractive to me include Netbeans IDE, video support, production suite. I downloaded and try the IDE, it is very simple to build your first “hello world” JavaFX program.

Refer to JavaFX is live