3 Replies Latest reply: Jun 6, 2013 8:49 AM by 1005510 RSS

    ManagerBean and Web Server VS simple browser and controller class on JavaFX

    1005510
      Good afternoon,

      Please I want to know if it is possible to do the following.
      I have a small JavaFX application and in one of the interfaces I have a browser and I load on it a map from google maps.
      I want that the information in the map to be loaded from the database each x seconds.
      If I make a thread and a loop in the controller class, the interface is blocked because the condition of the loop is true.

      The solution is possible with managedBean and glassfish like on this script that is contained in an xhtml page
      the function CarOnTime is executed each 2 seconds and calls a ManagedBean
            <script type="text/javascript">
      
                                              var latlng = new google.maps.LatLng(35.857908, 10.598997);
                                              var Options = {
                                                  zoom: 15,
                                                  center: latlng,
                                                  mapTypeId: google.maps.MapTypeId.ROADMAP
                                              };
                                              var map = new google.maps.Map(document.getElementById("map_canvas"), Options);
                                              var carMarkerImage = new google.maps.MarkerImage('resources/images/car.png');
                                              var marker = new google.maps.Marker({
                                                  position: new google.maps.LatLng(35.857908, 10.598997),
                                                  map: map,
                                                  draggable: false,
                                                  //icon: carMarkerImage,
                                                  title: "Car on Time",
                                                  autoPan: true
      
                                              });
                                              var infobulle = new google.maps.InfoWindow({
                                                  content: "Le service n\'est pas disonible !!"
                                              });
                                              google.maps.event.addListener(marker, 'mouseover', function() {
                                                  infobulle.open(map, marker);
                                              });
                                              function carOnTime() {
                                                  $.ajax({url: "getCarsGeo", context: document.body})
                                                          .done(function(data) {
                                                      vars = data.split("|");
                                                      marker.setPosition(new google.maps.LatLng(vars[2], vars[3]));
                                                      marker.setIcon(carMarkerImage);
                                                      map.setCenter(new google.maps.LatLng(vars[2], vars[3]));
                                                      infobulle.content = "Le matricule est : " + vars[0];
      
                                                  });
                                                  setTimeout("carOnTime()", 2000);
      
                                              }
                                              ;
      
                                              carOnTime();
      
      
                                          </script>
      this is what I have done, with thread and the interface is blocked when executing.
      public class OnTimeController implements Initializable {
      
          MyBrowser myBrowser;
          WebEngine we;
          double latitude;
          double longitude;
          Service service;
          Client client;
          @FXML
          private AnchorPane anchorMap;
          @FXML
         
          String selectedCarId = "1";
      
          @Override
          public void initialize(URL url, ResourceBundle rb) {
              
              myBrowser = new MyBrowser();
              anchorMap.getChildren().add(myBrowser);
              we = myBrowser.getWebEngine();
      
              FxThread t = new FxThread();
              while(true){
                  t.start();
                  try {
                      FxThread.sleep(2000);
                  } catch (InterruptedException ex) {
                      Logger.getLogger(OnTimeController.class.getName()).log(Level.SEVERE, null, ex);
                  }
              }
          }
      
          public void getOnTime() {
              System.out.println("THREAD EXECUTE");
              service = new Service("http://" + model.getAddIP() + ":8080/ServerGeoTrack/webresources", "authlogin/getParamOnTimeByCarId/" + selectedCarId, client);
              List<Paramontime> lp = service.getParamOnTimeByCarId();
              latitude = lp.get(0).getLatitude();
              longitude = lp.get(0).getLongitude();
      
              we.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
                  @Override
                  public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State t1) {
                      we.executeScript("goToLocation(" + latitude + "," + longitude + ")");
                  }
              });
          }
      
         
      
          public class FxThread extends Thread {
      
              @Override
              public void run() {
      
                  Platform.runLater(new Runnable() {
                      @Override
                      public void run() {
                          getOnTime();
      
                      }
                  });
      
      
      
      
              }
          }
      
             class MyBrowser extends Region {
      
                  HBox toolbar;
                  WebView webView = new WebView();
                  WebEngine webEngine = webView.getEngine();
      
                  public WebEngine getWebEngine() {
                      return webEngine;
                  }
      
                  public MyBrowser() {
      
                      final URL urlGoogleMaps = getClass().getResource("googlemaps.html");
                      webEngine.load(urlGoogleMaps.toExternalForm());
      
                      getChildren().add(webView);
      
                  }
              }
          }
      Is there annother solution with threads or a solution similar of using managedBeans, or calling the controller class and its method from the script.
      Thank you