F3 and JSR-223
By user12610627 on Nov 25, 2006
Here's a very simple example:
import javax.script.Bindings; import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import java.util.Date; ... ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByExtension("f3"); ScriptContext context = engine.getContext(); Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE); bindings.put("now:java.util.Date", new Date()); String script = "import java.util.Date;" + "import java.lang.System;" + " " + "System.out.println(now:Date);"; engine.eval(script); ...
The above example creates a
java.util.Date object in Java code and then makes it accessible to the F3 script. The F3 script simply prints the value of the date to standard out.
You make Java objects accessible to F3 code by using JSR-223 API's to assign such objects to F3 global enumerations. The format of the binding name is
This has the effect of creating a named instance of the specified class with the specified name.
Since F3 classes can implement Java interfaces, the recommended approach is to create a small number of such bindings, which represent the entry points between your Java and F3 code. Then you can use callbacks through Java interfaces to directly call F3 from Java (or Java from F3) rather than going through JSR-223 each time.
In the context of GUI development, since F3 user-interface components are underlyingly Swing JComponents, this makes it possible to add JComponents created in Java code to F3 widgets, or to embed F3 widgets into JComponents created in Java code.