Planet JFX

Updating Java Objects From JavaFX Script

118pages on
this wiki
Add New Page
Talk0 Share

Introduction Edit

The following code example will show you how to update a Java object from a JavaFX script.

package fxexamples;

public class DataObject {
private String name;
private String address;
private String phone;
private boolean isDirty;
/*Getters and Setters omitted for brevity*/

        public void PrintContents() {
		System.out.println("Name: " + name);
		System.out.println("Address: " + address);
		System.out.println("Phone: " + phone);


package fxexamples;
import javafx.ui.*;
import java.lang.System;
import fxexamples.DataObject;


class DataClass {
 attribute name:String;
 attribute address:String;
 attribute phone:String;

var data:DataClass = new DataClass();

operation UpdateJavaData(dc:DataClass) {
 System.out.println("Updated java object");

Frame {
 title: "FXTimer"
 width: 250
 height: 200
 content: [ Box {
 orientation: VERTICAL
 content: [ FlowPanel {
 content: [ Label {text: "Name"}, TextField { value: bind columns: 10 } ]
 }, FlowPanel {
 content: [ Label {text: "Address"}, TextField { value: bind data.address columns: 10 } ]
 }, FlowPanel {
 content: [ Label {text: "Phone"}, TextField { value: bind columns: 10 } ]
 }, Button {
 text: "Update Java Data" action: operation() {
 visible: true

package fxexamples;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleScriptContext;
import javax.swing.SwingUtilities;

public class RunFX {
 public static void main(String[] args) {
 final DataObject myObj = new DataObject();
 Runnable fxScript = new Runnable() {
 public void run() {
 ClassLoader loader = Thread.currentThread().getContextClassLoader();
 ScriptEngineManager manager = new ScriptEngineManager(loader);
 ScriptEngine engine = manager.getEngineByExtension("fx");
 Bindings bindings = engine.createBindings();
 //Bind the Java Object to the one we'll use in the JavaFX Script
 bindings.put("JAVAOBJ:fxexamples.DataObject", myObj);
 ScriptContext context = new SimpleScriptContext();
 context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
 context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
 String script = "import fxexamples.FXtoJava;";
 try {
 } catch(Exception ex) {
 SwingUtilities.invokeLater( fxScript );

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.