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
 

Comments:

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

Posted by Michael Heinrichs on April 01, 2008 at 10:24 AM CEST #

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.

Posted by Ahmed Al-Hashimi on June 11, 2008 at 05:28 AM CEST #

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?

Posted by Stevie on August 14, 2008 at 04:31 PM CEST #

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)"

Posted by wyz on September 10, 2008 at 12:04 AM CEST #

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

Posted by wyz on September 10, 2008 at 12:07 AM CEST #

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

Posted by Mudit Sharma on September 18, 2008 at 07:33 AM CEST #

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.

Posted by Chris DeLashmutt on October 02, 2008 at 08:50 PM CEST #

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

Posted by Michael Heinrichs on October 03, 2008 at 03:35 AM CEST #

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.

Posted by Andrew Hughes on October 13, 2008 at 10:17 PM CEST #

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??

Posted by jx_noob on November 30, 2009 at 06:35 AM CET #

Post a Comment:
Comments are closed for this entry.
About

Michael Heinrichs

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today