2017-07-26 81 views
0

我有一個瀏覽器裏面的Javafx項目。我在瀏覽器中使用angularJS。當我嘗試從後臺線程轉到新狀態時,視圖不會執行它,直到我與其中一個按鈕進行交互。所以角碼被調用,但視圖被凍結,直到點擊某物。從後臺線程更新javaFX瀏覽器內容

Task task = new Task<Void>() { 
     @Override public Void call() { 
      while (true) { 
       try { 
        System.out.println("sLEEPING"); 
        Thread.sleep(4000); 
        System.out.println("Reloading page"); 
        Platform.runLater(() -> { 
         communicator.changeUI(); 

        }); 
        System.out.println("pAGE Reloaded"); 

       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }; 
    Thread t = new Thread(task); 
    t.start(); 

回答

1

不更新從後臺線程UI的狀態(見documentation了「線程」一節)。將實際更改UI的代碼封裝在Platform.runLater()(或使用TaskonSucceeded處理程序)。

例如爲:

// Runs in some background thread: 
public class MyRunnable { 

    @Override 
    public void run() { 
     // long-running code here... 

     // to update UI: 
     Platform.runLater(() -> { 
      // update UI here... 
     }); 
    } 
} 
+0

我說我的代碼,我使用Platform.runLater(),但認爲仍然在等待一個點擊。我在想也許我需要告訴瀏覽器做一些更新?不是實際重新加載 –