passing values to JavaFX from javascript

2020-02-15 04:35发布

As this is my first post , forgive any inaccuracies. My problem , i want to pass the value from an javascript function to Javafx on the initialize method

public void initialize(URL arg0, ResourceBundle arg1) {

    // TODO Auto-generated method stub
    final URL urlGoogleMaps = getClass().getResource("googlemaps.html");
    System.out.println("fjdsoij");
    browser.getEngine().load(urlGoogleMaps.toExternalForm());
    WebEngine webEngine = browser.getEngine(); 
    webEngine.getLoadWorker().stateProperty().addListener(
            new ChangeListener<State>(){

                @Override
                public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
                    if(newState == State.SUCCEEDED){
                        JSObject window = (JSObject)webEngine.executeScript("window");
                        window.setMember("app", new JavaApplication());

                    }
                }
            });
}

public class JavaApplication{
    public void calljavascript( int lengthInMeters){

        System.out.println(lengthInMeters);
    }
}   

}

and the javascript part is like this

   function initialize() {
  var mapOptions = {
    zoom: 3,
    center: new google.maps.LatLng(0, -180),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  var flightPlanCoordinates = [
    new google.maps.LatLng(37.772323, -122.214897),
    new google.maps.LatLng(21.291982, 157.821856)

  ];
  var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
     //geodesic: true,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2
  });
var lengthInMeters = google.maps.geometry.spherical.computeLength(flightPath.getPath());

  flightPath.setMap(map);
   alert("polyline is "+lengthInMeters+" long");
   app.calljavascript(lengthInMeters);
}

google.maps.event.addDomListener(window, 'load', initialize);

The value i want to pass is the lengthInMeters on the method calljavascript any ideas ? thnx in advance

1条回答
小情绪 Triste *
2楼-- · 2020-02-15 05:09

I've tried a simplified version of your code:

    WebEngine webEngine = browser.getEngine();

    webEngine.getLoadWorker().stateProperty().addListener((ov,oldState,newState)->{
        if(newState==State.SUCCEEDED){
            JSObject window = (JSObject) webEngine.executeScript("window");
            window.setMember("app", new JavaApplication());
        }
    });
    webView.getEngine().loadContent("<html>\n"
            + " <script>function initialize() {"
            + " var lengthInMeters = 5; " 
            + " app.calljavascript(lengthInMeters);"
            + "} </script> "
            + "    <body onLoad=\"initialize()\">Hi!\n"
            + "    </body>\n"
            + "</html>");

and it's not working.

In your case and in my approach, setMember() is called after the web has been loaded, so initialize() is called before by the load method. Consequently, app.calljavascript() fails.

The solution is this:

    WebEngine webEngine = browser.getEngine();
    JSObject window = (JSObject) webEngine.executeScript("window");
    window.setMember("app", new JavaApplication());

    browser.getEngine().loadContent("<html>\n"
            + " <script>function initialize() {"
            + " var lengthInMeters = 5; " 
            + " app.calljavascript(lengthInMeters);"
            + "} </script> "
            + "    <body onLoad=\"initialize()\">Hi!\n"
            + "    </body>\n"
            + "</html>");

Notice we set the member before the web content is loaded.

EDIT

I've created a more elaborated answer here.

查看更多
登录 后发表回答