Chris Oliver's Weblog
- All
- F3
- JavaFX
- Programming
- Research
F3 and JSR-223
F3 provides a JSR-223 (Scripting for the Java Platform) compliant interface in order to interact with F3 from Java code.
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
Name:Class
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.
Posted at 09:07AM Nov 25, 2006 by Christopher Oliver in F3 | Comments[3]
Do you have an example on how to embed a F3 widget into a Java swing panel?
I have created a JPanel in java, and I am now trying to embed a F3 Label into it - with no success. I am just discovering F3 (and I am not much experienced with Swing) - so I assume I must miss something obvious....
Here the piece of F3 code:
import javax.swing.*; import java.lang.*; import javafx.ui.*; import javafx.ui.canvas.*; import javafx.ui.filter.*; var lab1 = Label { text : "Hello" }; testpanel:JPanel.add(lab1:JComponent);and the returned exception: Note that the following works:testpanel:JPanel.add(new JLabel("Grrr!"));Cheers! F3 looks great!
-- daniel
Posted by daniel on May 16, 2007 at 06:37 AM PDT #
http://www.oreillynet.com/onjava/blog/2007/05/javafx_first_steps_hello_onjav_1.html
The trick is to do:
var lab1 = Label { text : "Hello" }; testpanel:JPanel.add(lab1.getComponent());-- danielPosted by daniel on May 16, 2007 at 07:11 AM PDT #
Posted by panga on July 26, 2007 at 08:16 AM PDT #