Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8089260

Calling java function with array arguments from javascript fails

XMLWordPrintable

    • web

      Hi all,

      When using a WebEngine and calling from Javascript a Java function having an argument being an array of a basic type (String[], int[], double[], etc.), the argument being an array of basic type is always null.

      To reproduce the error run the code below:

      ---- Begin code

      import javafx.application.Application;
      import javafx.beans.value.ChangeListener;
      import javafx.beans.value.ObservableValue;
      import javafx.concurrent.Worker;
      import javafx.scene.web.WebEngine;
      import javafx.stage.Stage;
      import netscape.javascript.JSObject;


      public class Main extends Application {

          public static void main(String[] args) {
              Application.launch(Main.class);
          }

          public class ParentFunctions {
              public void parentfunc(String title, String[] args) {
                  System.out.println(title);
                  System.out.println(args);

                  assert (title != null) : "title must not be null";
                  assert (args != null) : "args must not be null";
              }
          };

          @Override
          public void start(Stage stage) throws Exception {
              final WebEngine engine = new WebEngine();
              engine.loadContent("<script type='text/javascript'>function init(javaobj) { javaobj.parentfunc('title', ['arg1', 'arg2']); }</script>");

              engine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
                  @Override
                  public void changed(final ObservableValue<? extends Worker.State> observableValue, final Worker.State oldState, final Worker.State newState) {
                      if (newState == Worker.State.SUCCEEDED) {
                          JSObject window = (JSObject) engine.executeScript("window");
                          window.call("init", new Object[]{new ParentFunctions()});
                      }
                  }
              });
          }
      }

      ---- End code

      Does JavaFX WebEngine allows passing JavaScript array from JavaScript to Java ?

      Thanks

            arajkumar Arunprasad Rajkumar
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: