Recent changes Random page
GAMING
Technology
 
Gaming
Entertainment
Science Fiction
Biggest wikis
Hobbies
Music
See more...

Programmatically invoking a JavaFX Script

From Planet JFX

Jump to: navigation, search

There are two ways to programmatically launch a JavaFX Script.

[edit] Using FXShell

The simplest, though unsupported, way is by calling the main() method in the FXShell class provided by the JavaFX jars:

FxMainLauncher.java:

import net.java.javafx.FXShell;

public class FxMainLauncher {
    public static void main(String[] args) throws Exception {
        FXShell.main(new String[] {"HelloWorld.fx"});
    }
}

[edit] Using JSR-223

A more flexible way is to use JSR-223 Script Engine support to invoke JavaFX as a scripting language. This solution is more flexible in that it allows for objects to be sent to JavaFX via the Bindings object (see line tagged with Note-1).

This example requires either JDK 1.6 or a JSR-223 implementation (a reference implementation is available at the above link). For more information, read this introduction to Scripting for the Java Platform.

As you start to branch out into multiple files, this approach changes a bit; see Referencing other JavaFX files for more details.

FxScriptLauncher.java:

import java.io.InputStreamReader;
import java.util.Date;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleScriptContext;

public class FxScriptLauncher {
    public static void main(String[] args) throws Exception {
        // set up script:
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        ScriptEngineManager manager = new ScriptEngineManager(loader);
        ScriptEngine engine = manager.getEngineByExtension("fx");

        Bindings bindings = engine.createBindings();
        bindings.put("now:java.util.Date", new Date()); // Note-1

        ScriptContext context = new SimpleScriptContext();
        // Bug workaround
        context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
        context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        engine.setContext(context);

        InputStreamReader reader = new InputStreamReader(FxScriptLauncher.class
                .getResourceAsStream("HelloWorld.fx"));
        Object scriptReturnValue = engine.eval(reader);
        System.out.println(scriptReturnValue);
    }
}

A simpler version of this same class is shown below. In this version, the default bindings and context are used:

FxScriptLauncher2.java:

import java.io.InputStreamReader;
import java.util.Date;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class FxScriptLauncher2 {
    public static void main(String[] args) throws Exception {
        // set up script:
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByExtension("fx");
            
        engine.put("now:java.util.Date", new Date());           
        InputStreamReader reader = 
               new InputStreamReader(FxScriptLauncher2.class
               .getResourceAsStream("HelloWorld.fx"));
        Object scriptReturnValue = engine.eval(reader);
        System.out.println(scriptReturnValue);
    }
}

--Joconner 07:43, 21 July 2007 (UTC)

[edit] HelloWorld.fx source

A simple Hello World implementation, to complete the example. It should pull in the now object set in the Java code above:

HelloWorld.fx:

import javafx.ui.*;
import javafx.ui.canvas.*;
	
Frame {
    title: "Hello World!"
    content: Label {
        text: "Hello World with Java date: {now:<<java.util.Date>>}"
    }
    visible: true
    
}

The running application looks similar to the image shown here:

Image:Javadate.PNG

Rate this article:
Share this article:
.