2 Replies Latest reply: Mar 15, 2013 8:14 AM by peterz RSS

    background thread which periodically run WebEngine.executeScript

    user12956197
      hi all
      I need to use WebEngine.executeScript("some js function") to get user's status. if js function return some certain values. I will do something.
      here is my codes:

                final Button myBtn=new Button("Check condition");
                myBtn.setOnAction(new EventHandler<ActionEvent>() {
                     @Override
                     public void handle(ActionEvent event) {
                          myBtn.setDisable(true);
                          engine.executeScript("_CRAWLER_.captureSelectedElem();");          
                          MyService cs=new MyService (engine);
                          cs.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
                               
                               @Override
                               public void handle(WorkerStateEvent event) {
                                    String s=(String) event.getSource().getValue();
                                    System.out.println(s);

                                    myBtn.setDisable(false);
                               }
                          });
                          cs.setOnFailed(new EventHandler<WorkerStateEvent>() {
                               
                               @Override
                               public void handle(WorkerStateEvent event) {
                                    myBtn.setDisable(false);
                               }
                          });
                          cs.start();
                     }
                });

      class MyService extends Service<String>{

           private WebEngine engine;
           public MyService (WebEngine engine){
                this.engine=engine;
           }
           @Override
           protected Task<String> createTask() {
                return new Task<String>(){

                     @Override
                     protected String call() throws Exception {
                          
                          String ret=null;
                          try{
      //exception here               ret=(String)engine.executeScript("some js function();");
                          }catch(Exception e){
                               e.printStackTrace();
                          }
                          int retry=0;
                          while(ret==null){
                               if(retry>10) break;
                               ret=(String)engine.executeScript("_CRAWLER_.captureSelectedElem();");
                               try {
                                    Thread.sleep(500);
                               } catch (InterruptedException e) {
                                    logger.error(e.getMessage(),e);
                               }
                          }
                          return ret;
                     }
                     
                };
                
           }
           
           
      }

      but it throws java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-18 in