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.

Comments:

Hi Chris!

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:
net.java.javafx.typeImpl.TypeFactoryImpl$ModuleImpl$ThrownException: testpanel:JPanel.add(lab1:JComponent)
Note that the following works:
testpanel:JPanel.add(new JLabel("Grrr!"));


Cheers! F3 looks great!

-- daniel

Posted by daniel on May 15, 2007 at 11:37 PM PDT #

OK, I found it here:
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());
-- daniel

Posted by daniel on May 16, 2007 at 12:11 AM PDT #

Great Chris! How to supply the additional jarfiles to the script engine.

Posted by panga on July 26, 2007 at 01:16 AM PDT #

Simple and Nice example !

Posted by شات on December 15, 2010 at 04:07 AM PST #

Editor sicherlich etliche Pluspunkte verd

Posted by beko on January 21, 2011 at 08:18 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

user12610627

Search

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