2014-11-03 81 views
0

由於這是我的第一篇文章,請原諒任何不準確的地方。我的問題,我想從一個JavaScript函數傳遞價值的JavaFX在初始化方法將值從javascript傳遞給JavaFX

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); 
    } 
} 

}

和JavaScript的部分是這樣的

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); 

我想要的價值關於callJavascript方法中的lengthInMeters是否有任何想法?日Thnx提前

回答

0

我試過你的代碼的簡化版本:

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>"); 

,它不工作。

在你的情況和我的方法,setMember()被稱爲後的網頁已被加載,所以initialize()load方法稱爲之前。因此,app.calljavascript()失敗。

解決的辦法是這樣的:

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>"); 

通知我們設置的部件裝載的Web內容之前

編輯

我創建了一個比較詳細的答案here

+0

yap那是正確的解決方案現在一切正常,謝謝你:) – n00bfighter 2014-11-03 21:59:58

+0

這不適用於隨後的加載調用。在加載之前無法設置成員。有更好的解決方案嗎? – mohamnag 2016-02-29 23:11:10

+0

請發表一個新的問題引用這個解決方案 – 2016-03-01 08:04:18