B4J Question How are B4J classes called from Nashorn?

Bruce Axtens

Active Member
Licensed User
If you look at the code to BOLE (in MENU\1-File\2-Open.js) you'll see where I've tried to call a B4J class from "outside" in Nashorn. All methods in the class file have a leading capital letter. However, when calling from Nashorn, I've had to add a leading underscore and lowercase the name of the method. (Open becomes _open, Read becomes _read etc.) Also, I've had to include as the first parameter of the call, the name of the class itself.

var f = FileControl._open(FileControl, "./", "All Files", "*.*");
if ("" !== f) {
MainText.text = FileControl._read(FileControl, f);
gsPath =f;
}​

The class is instantiated as

Private oFC As fileControl
oFC.Initialize(MainForm)
and exposed to Nashorn as

oNas.enginePut("FileControl",oFC)
(the source can be seen from the github repository)

Questions:
  • Is this how it's supposed to be?
  • Is there a way of declaring the class's methods and properties such that Nashorn doesn't have to have the class name as a first parameter?
  • Would it have been better to have make a library (jar + xml)? or
  • do I need to go into pure java for this kind of thing?
  • Could I have done the FileChooser purely from the Nashorn side? And if yes, how?
 

Bruce Axtens

Active Member
Licensed User
I wasn't sure how to select a file from within a Nashorn script, so I tried, with some success despite the oddities of the calling convention, to do so via B4J. Once I figure out how to call FileChooser from Java core, I won't need to call it via B4J.

http://www.javaworld.com/article/21...nashorn--javascript-made-great-in-java-8.html would seem to be a good place to start.

As for the purpose, it's all part of BOLE's approaching to menus, namely that every element in the menu system be implemented in script rather than be locked up in the main binary.
 
Last edited:

Bruce Axtens

Active Member
Licensed User
By the way, can you unpack what you mean by "deployment mode."?
 

Bruce Axtens

Active Member
Licensed User
Hmm ... can that behaviour be handled with #If etc?
 

Bruce Axtens

Active Member
Licensed User
Can't say you're ambiguous in your answers. :) Okay, well, just have to figure out how to call FileChooser direct from Nashorn.
 

Bruce Axtens

Active Member
Licensed User
Finally getting somewhere with FileChooser from Nashorn

B4X:
    var Stage = Java.type("javafx.stage.Stage");
    var FileChooser = Java.type("javafx.stage.FileChooser");
    var st = new Stage;
    var fc = new FileChooser;
    var f = fc.showOpenDialog(st);
    MessageBox.Show(    (f),gsVersion);
 
Top