Java Reference
In-Depth Information
if (engine == null) {
System.out.println(
"no scripting engine available");
}else {
engine.put("name", "Eric");
engine.put("age", 25);
Object fxObj = null;
try {
fxObj = engine.eval(script);
} catch (ScriptException ex) {
ex.printStackTrace();
}
System.out.println("JavaFX Object = " + fxObj);
if(fxObj != null) {
System.out.println("JavaFX Class = "+
fxObj.getClass());
}
}
}
}
The result of running this program produces the Student with the values for
name and age of “Eric” and 25, respectively.
Name = Eric, age = 25
JavaFX Object = ___FX_SCRIPT___$Student@1083717
JavaFX Class = class ___FX_SCRIPT___$Student
Java Scripting API with Compilation
Let's say we want to reuse the Student object and invoke methods on it. You do
this by first compiling the object, setting the bindings, then creating an instance
of the object. After we have the object instance, we can invoke methods on the
Student object.
First, let's modify the script to add an instance function, getName() , as shown in
Listing 11.3.
Listing 11.3
Java Code - Sample JavaFX Script for Java Scripting API
String script =
"public class Student {\n" +
" public var name:String;\n" +
" public var age:Integer;\n" +
" public function getName ():String { \n" +
 
 
Search WWH ::




Custom Search