Java Reference
In-Depth Information
button.onAction = function() {
print("Adding Car:" + makeText.text);
car.make = makeText.text;
car.model = modelText.text;
car.year = yearText.text;
car.description = descriptionText.text;
carList.add(car);
carCount = "The number of cars is: " + carList.size();
print(carCount);
};
grid.add(button, 0, 5);
primaryStage.scene = new Scene(grid, 800, 500);
primaryStage.show();
How It Works
The Nashorn engine has full access to the JavaFX API. This means that it is possible to
construct JavaFX applications that are written either entirely or partially in JavaScript.
The two solutions to this recipe demonstrate each of these techniques. The first solu-
tion demonstrates how to develop a JavaFX application entirely of JavaScript. When
you're using the technique demonstrated in Solution 1, the JavaScript implementation
can be executed by using the jjs tool and specifying the -fx option, as follows:
jjs -fx recipe18_11.js
Solution 2 demonstrates how to construct a JavaFX application from Java code,
embedding the implementation code written in JavaScript. To use this technique, con-
struct a standard JavaFX application class by extending the
javafx.application.Application class and overriding the start() meth-
od. Within the start() method, create a Nashorn ScriptEngine object and use it
to embed a JavaScript file that contains the application implementation. Prior to calling
the engine's eval() method and passing the JavaScript file, pass the JavaFX stage to
the engine using the engine's put() method.
engine.put("primaryStage", stage);
Search WWH ::




Custom Search