Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
811 views
in Technique[技术] by (71.8m points)

java - passing values to JavaFX from javascript

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>
"
            + " <script>function initialize() {"
            + " var lengthInMeters = 5; " 
            + " app.calljavascript(lengthInMeters);"
            + "} </script> "
            + "    <body onLoad="initialize()">Hi!
"
            + "    </body>
"
            + "</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>
"
            + " <script>function initialize() {"
            + " var lengthInMeters = 5; " 
            + " app.calljavascript(lengthInMeters);"
            + "} </script> "
            + "    <body onLoad="initialize()">Hi!
"
            + "    </body>
"
            + "</html>");

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

EDIT

I've created a more elaborated answer here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...