2

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 Answers1

1

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.

Community
  • 1
  • 1
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • yap that was the solution exactly now everything works fine , thank you man :) – n00bfighter Nov 03 '14 at 21:59
  • 1
    this does not work on consequent load calls. it is not possible to set the members before loading any more. is there a better solution? – mohamnag Feb 29 '16 at 23:11
  • Please, post a new question refering to this solution – José Pereda Mar 01 '16 at 08:04
  • IMPORTANT NOTE: There's a [bug](https://stackoverflow.com/questions/41903154/javafx-webview-callback-from-javascript-failing-after-garbage-collection) since Java 8u101. `new JavaApplication()` should be defined in a private field to prevent the Garbage Collection from deleting it. A local variable will seem to work at first but will randomly stop working. – Eric Duminil Mar 07 '18 at 21:37
  • I've having the same issue as @mohamnag, the bridge works for the first page load but not subsequent ones. – Guillaume F. Dec 29 '19 at 19:51