Creating JavaFX objects in Java programs

So far, all of the examples in my previous article about how to use JavaFX objects in Java code expected the object as an input parameter. But what if you want to create a JavaFX object directly? In this article, I will describe a very simple but effective solution.

(Edit: As Mike Azzi pointed out to me (thanks again!), I should have mention, that it is also possible to create JavaFX objects by using the compiled Java classes directly. I plan to cover this later, in a series of articles about what happens during compilation.)

Let’s assume we want to create an instance of MyJavaFXClass as it was defined in the previous article. Code sample 1 shows the implementation of the class.

 1 import java.lang.System;
 2 
 3 public class MyJavaFXClass {
 4 
 5     public attribute property: String;
 6 
 7     public function printProperty() {
 8         System.out.println (property);
 9     }
10 }

Code Sample 1: Definition of MyJavaFXClass

To create an instance of this class in JavaFX Script, we would usually define an object literal. We can do that as well in Java and pass the object literal to the JavaFX-script engine we already encountered in the previous article. The script engine can parse the object literal and return a new instance of the class.

Code sample 2 shows a Java class, which creates an instance of MyJavaFXClass and uses the script engine to call its function printProperty.

 1 import javax.script.ScriptEngineManager;
 2 import com.sun.javafx.api.JavaFXScriptEngine;
 3 
 4 public class Main {
 5 
 6     public static void main (String[] args) {
 7         ScriptEngineManager manager = new ScriptEngineManager();
 8         JavaFXScriptEngine fxEngine = 
 9                 (JavaFXScriptEngine) manager.getEngineByName ("javafx");
10 
11         try {
12             Object o = fxEngine.eval
13                     ("MyJavaFXClass { property: \"JavaFX class created in Java\" }");
14             fxEngine.invokeMethod (o, "printProperty");
15         } catch (Exception ex) {
16             ex.printStackTrace();
17         }       
18     }
19 }

Code Sample 2: Constructing MyJavaFXClass in Java program

11 responses to “Creating JavaFX objects in Java programs”

  1. Michael Heinrichs Avatar
    Michael Heinrichs

    In his blog, Jim Clarke extended the example above to demonstrate, how to retrieve error messages during the compilation. You can find his post here:
    http://blogs.sun.com/clarkeman/entry/javafx_scripting_api

  2. Ahmed Al-Hashimi Avatar

    It is very cool…….
    but why is not the ScriptEngine Class usied directly instead of JavaFXScriptEngine

    as using java script or JRUBY……
    ScriptEngineManager e = new ScriptEngineManager()
    ScriptEngine je = e.getEngineByName ("javascript");

    Regards.

  3. Stevie Avatar
    Stevie

    Hi Michael,

    I tested your example, and it the "Main" class compiled just fine, but when I tried to run it, I got the following exception:
    Exception thrown in JavaFX pretty printing: java.io.FileNotFoundException: tmp___FX_SCRIPT___.fxdump from StringInputBuffer

    Do you have any idea of I have done wrong?

  4. wyz Avatar
    wyz

    Hi Michael, I have the same problem with Stevle. However, if I try to eval the actual JavaFX code or put the ".fx" file as a input stream, it will work per normal, but I see exception reads:
    "Exception thrown in JavaFX pretty printing: java.io.FileNotFoundException: tmp___FX_SCRIPT___.fxdump from StringInputBuffer (The system cannot find the path specified)"

  5. wyz Avatar
    wyz

    I forgot to indicate my environment. I was using Netbeans 6.1 JavaFX edition, in a Windows XP Virtual Box VM.

  6. Mudit Sharma Avatar
    Mudit Sharma

    hi Michael,
    i’m facing same problem as wyz on similar environment. Could you please suggest solution to problem.

  7. Chris DeLashmutt Avatar
    Chris DeLashmutt

    I see this problem as well, and I actually traced through the script code. It seems that the script compiler is making some assumptions about temporary directories instead of using the proper APIs to grab the temp directory. Check out line 123 in com.sun.tools.javafx.script.JavaFXScriptCompiler from the source code.

  8. Michael Heinrichs Avatar
    Michael Heinrichs

    Thanks a lot Chris. I fixed the issue – right in time for the V1.0 release.

  9. Andrew Hughes Avatar
    Andrew Hughes

    Hi Michael, you mention that you would in future discuss the binding of Java Objects in JavaFX. I’d be really interested to see how this is done? My application (like many) receive’s a lot of external stateful input from an existing java codebase. How can JavaFX binding’s be used in this situation? Typically, a developer spends a lot of time writing swing boilerplate code (and even more time debugging it).

    Thanks for the words of wisdom.

  10. jx_noob Avatar
    jx_noob

    i try this example and get this exceptions:

    Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/javafx/api/JavaFXScriptEngine
    at test.MyLibraryByScriptEngine.main(MyLibraryByScriptEngine.java:18)
    Caused by: java.lang.ClassNotFoundException: com.sun.javafx.api.JavaFXScriptEngine
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

    can you helb me??

  11. to domain name

    hi guys…

    hi guysI would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well and i have start my own blog now, , thanks for your effort…