Skip to main content

the fxjs logo - Java Interop

APIs for JVM Languages

the fxjs logo also has an API to enable Java (and other JVM language) host programs to launch or load a the fxjs logo script. The API class fxjs.Loader is also contained in the same JAR file FXJS.jar, and is described further below.

launch(): Launching a Script

Host programs can launch a the fxjs logo scrpt by invoking the public static void launch() function of the class fxjs.Loader. It takes the following arguments:

  • script: a String containing the user-defined the fxjs logo script
  • args: a possibly empty array (String[]) containing arguments for any main() or main2() function in the script
  • user: a possibly null reference to an Object that the script can access as this.apiUser. This is one way of passing a controller to the script.
  • silent: a boolean flag that suppresses informational messages when set to true

fxjs.Loader.launch() loads the script by invoking fxjs.Loader.load(), described below, and then invokes the launch() function of an internally provided Application object. The start() function of this Application object is overridden to manipulate its primaryStage parameter to display the scene graph returned from the invocation of fxjs.Loader.load().

The root node of the the fxjs logo script passed to fxjs.Loader.launch() does not necessarily have to be a Stage or a Scene. Any subclass of Parent will be successfully displayed by dressing it up as required.

Unlike FXMLLoader's load() family of functions, fxjs.Loader.launch() itself displays the GUI objects in the script, and remains blocked till the JavaFX window it presents is closed. The following example shows the launch() function in use. The script file is expected to be found at the location samples/UsingFXML.js on the classpath.

package samples;

import java.io.InputStream;
import java.io.IOException;
import fxjs.Loader;

public class UsingFXML {

    private String getFXML() throws IOException {
        InputStream is = ClassLoader.getSystemResourceAsStream("samples/UsingFXML.js");
        byte[] buf = new byte[is.available()];
        is.read(buf);
        return new String(buf);
    }

    private void go(String[] args) throws Exception {
        String fxJS = getFXML();
        Loader.launch(fxJS, args, this, false);
    }

    public void action(String a) {
        System.out.printf("action: %s%n", a);
    }

    public static void main(String[] args) throws Exception {
        UsingFXML me = new UsingFXML();
        me.go(args);
    }
}

Observe that the program passes itself (this) to the script as the controller. This enables the script to call the program's action(String) function. The script is shown below, and should be placed in a file that can be found at the location samples/UsingFXML.js on the classpath.

{T: fxStage, title: 'JavaFX Welcome',
  scene: {T: fxScene, width: 300, height: 275, 
    root: {T: fxGridPane, vgap: 10, hgap: 10, alignment: fxCENTER,
        padding: {T: fxInsets, top: 25, right: 25, bottom: 25, left: 25}, 
      children: [
        {T: fxText, id: 'welcome-text', text: 'Welcome',
            font: {T: fxFont, name: 'Tahoma', size: 20},
            rowIndex: 0, columnIndex: 0, columnSpan: 2, rowSpan: 1},
        {T: fxLabel, text: 'User Name:', rowIndex: 1, columnIndex: 0},
        {T: fxTextField, fxid: 'name', rowIndex: 1, columnIndex: 1},
        {T: fxLabel, text: 'Password:', rowIndex: 2, columnIndex: 0},
        {T: fxPasswordField, fxid: 'pass', rowIndex: 2, columnIndex: 1},
        {T: fxHBox, spacing: 10, rowIndex: 4, columnIndex: 1, 
            alignment: fxBOTTOM_RIGHT,
            children: [{T: fxButton, text: 'Sign in', onAction: function (e) {
                this.apiUser.action(this.name.getText() + "/" + this.pass.getText());
                this.actiontarget.setFill(fxFIREBRICK);
                this.actiontarget.setText('Sign in button pressed');}}]},
        {T: fxText, fxid: 'actiontarget', rowIndex: 6, columnIndex: 1},
      ]
    }
  }
}

Both the files shown above are also bundled in FXJS.jar.

load(): Loading a Script

Host programs can load a the fxjs logo script (translate a string into a JavaFX object graph) by invoking the public static Object load() function of the class fxjs.Loader. It takes the following arguments:

  • script: a String containing the user-defined the fxjs logo script
  • args: a possibly empty array (String[]) containing arguments for any main() or main2() function in the script
  • user: a possibly null reference to an Object that the script can access as this.apiUser. This is one way of passing a controller to the script.
  • silent: a boolean flag that suppresses informational messages when set to true

There is currently no common host-program use-case for this function, but it is provided as a stepping stone for programs being migrated from FXMLLoader. Using launch(), described above, should be the preferred approach for all host programs.

Threading Issues

The main() and main2() functions in a the fxjs logo script (if used) are both executed by the JavaFX Application Thread. The same thread also executes all initialization code till the GUI becomes visible. Programs that require additional threads for background tasks should use the techniques described in Concurrency in JavaFX.

 
 
Close
loading
Please Confirm
Close