X
  • March 28, 2015

Combining Nashorn Scripting with JavaFX

Geertjan Wielenga
Product Manager

Following from yesterday's blog entry, the question I had was: "Can I use -scripting and -fx at the same time?"

The answer is yes:

 /* global $OUT, generator, FXCollections, javafx, $STAGE */
load("fx:base.js");
load("fx:graphics.js");
load("fx:controls.js");
$EXEC("C:/Users/gwieleng/AppData/Roaming/npm/yo.cmd");
var output = $OUT;
var cleanedOutput = [];
output = output.substring(81, output.length - 553).split("\n");
for each(generator in output){
    if (generator.length>0){
        cleanedOutput.push(generator);
    }
}
options = FXCollections.observableArrayList(cleanedOutput);
selectedGenerator = new Label();
selectedGenerator.setFont(javafx.scene.text.Font.font(24));
comboBox = new ComboBox(options);
comboBox.setValue(options[0]);
comboBox.valueProperty().addListener(new javafx.beans.value.ChangeListener({
    changed: function (observableValue, oldSelection, newSelection) {
        selectedGenerator.setText(newSelection);
    }
}));
var root = new VBox(10);
var box1 = new HBox(10);
var box2 = new HBox(10);
box1.children.addAll(new Label("Choose a generator: "), comboBox);
box2.children.addAll(selectedGenerator);
root.children.addAll(box1, box2);
$STAGE.title = "Yeoman Generator";
$STAGE.scene = new Scene(root, 280, 100);
$STAGE.show();

The above runs Yeoman on the command line via JavaScript, thanks to Nashorn, and displays the result in JavaFX:

And this is how the code above looks in NetBeans:


Indeed, this would appear to be the start of a shell script enabling Yeoman to be used from JavaFX.

By the way, to do the above, and to use the info in Adam's recent screencast and in my blog entry from yesterday, you need a NetBeans development build and then use the Nashorn tab that has been introduced there, i.e., this is not in NetBeans IDE 8.0.2 but in a development build for the next release:


Join the discussion

Comments ( 3 )
  • Chris Monday, March 30, 2015

    Is it possible, with such technic, to create plugins for Netbeans with JavaScript too? That would be so easy as with Cordova to create Plugins with HTML5, JS and CSS and wrap it around with a web view to get access to the netbeans api or like the example above.

    Regards

    Chris


  • Geertjan Monday, March 30, 2015

    Hi Chris, that's exactly what I've been looking at, here: https://blogs.oracle.com/geertjan/entry/extending_netbeans_with_nashorn


  • Chris Monday, March 30, 2015

    Hi Geertjan,

    nice, I will have a look at it soon :).

    Cheers,

    Chris


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services