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
I've tried a simplified version of your code:
and it's not working.
In your case and in my approach,
setMember()
is called after the web has been loaded, soinitialize()
is called before by theload
method. Consequently,app.calljavascript()
fails.The solution is this:
Notice we set the member before the web content is loaded.
EDIT
I've created a more elaborated answer here.