user3203690
user3203690

Reputation: 113

Passing info from JavaFX to Javascript

I'm trying to passing information(lattiude, longitude) to a google map in html webview. The problem is that the application don't start in netbeans. It returns that:

Executing C:\Users\Carlos\Documents\NetBeansProjects\OpenPilot\dist\run1883323097\OpenPilot.jar using platform C:\Program Files\Java\jdk1.8.0_05\jre/bin/java Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894) at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56) at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158) at java.lang.Thread.run(Thread.java:745) Caused by: javafx.fxml.LoadException: unknown path

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425) at openpilot.OpenPilot.start(OpenPilot.java:29) at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837) at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335) at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301) at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39) at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112) ... 1 more Caused by: netscape.javascript.JSException: TypeError: 'undefined' is not a function at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:128) at com.sun.webkit.WebPage.twkExecuteScript(Native Method) at com.sun.webkit.WebPage.executeScript(WebPage.java:1410) at javafx.scene.web.WebEngine.executeScript(WebEngine.java:934) at openpilot.FXMLDocumentController.initialize(FXMLDocumentController.java:74) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) ... 13 more Exception running application openpilot.OpenPilot Java Result: 1

Here the code:

public class FXMLDocumentController implements Initializable {

    @FXML public WebView Map;





    @Override
    public void initialize(URL url, ResourceBundle rb) {

        URL MapURL = getClass().getResource("map.html");
        WebEngine WebEngine = Map.getEngine();
        WebEngine.load(MapURL.toExternalForm());
        String test = "40.3130432088809";
       WebEngine.executeScript("getCoordinates('40.3130432088809')");

    }    

}

And here the html with the map:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
        var latitude;
        var longitude;
        function getCoordinates(latitude){
  this.latitude = latitude;

}
        var map;
        function initialize() {
            var mapOptions = {
                zoom: 8,
                center: new google.maps.LatLng(latitude, -2.900390625)
            };
        map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);

        google.maps.event.addListener(map, 'click', function(event) {
   placeMarker(event.latLng);
});


}
function placeMarker(location) {
    var marker = new google.maps.Marker({
        position: location, 
        map: map
    });

    google.maps.event.addListener(marker, 'rightclick', function() {
                    deleteMarker(marker);
                });

}

function deleteMarker(marker) {
                marker.setMap(null); 
            }
google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

Upvotes: 0

Views: 1600

Answers (1)

James_D
James_D

Reputation: 209225

You are almost certainly trying to execute the javascript before the page has loaded. You need to specify a handler to execute the script when the web engine has completed loading.

@Override
public void initialize(URL url, ResourceBundle rb) {

    URL MapURL = getClass().getResource("map.html");
    WebEngine WebEngine = Map.getEngine();
    WebEngine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> {
        if (newState == State.SUCCEEDED) {
            WebEngine.executeScript("getCoordinates('40.3130432088809')");
        }
    });
    WebEngine.load(MapURL.toExternalForm());

} 

Upvotes: 2

Related Questions