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;
 3 public class MyJavaFXClass {
 5     public attribute property: String;
 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;
 4 public class Main {
 6     public static void main (String[] args) {
 7         ScriptEngineManager manager = new ScriptEngineManager();
 8         JavaFXScriptEngine fxEngine = 
 9                 (JavaFXScriptEngine) manager.getEngineByName ("javafx");
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 Replies to “Creating JavaFX objects in Java programs”

  1. 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");


  2. 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: tmp___FX_SCRIPT___.fxdump from StringInputBuffer

    Do you have any idea of I have done wrong?

  3. 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: tmp___FX_SCRIPT___.fxdump from StringInputBuffer (The system cannot find the path specified)"

  4. 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 from the source code.

  5. 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.

  6. i try this example and get this exceptions:

    Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/javafx/api/JavaFXScriptEngine
    at test.MyLibraryByScriptEngine.main(
    Caused by: java.lang.ClassNotFoundException: com.sun.javafx.api.JavaFXScriptEngine

    can you helb me??

  7. Pingback: to domain name

Leave a Reply