2016-03-07 85 views
1

我有一個需要幾秒鐘處理的控制器內部的方法。我想在發生這種情況時彈出一個gif動畫,但我只能看到靜態圖像。這是我的代碼:在JAVAFX中處理時顯示gif動畫彈出框

@FXML 
public void search(ActionEvent e) { 


    final Stage dialog = new Stage(); 
    Group popup = new Group(); 
    Image image = new Image("file:resources/images/bender.gif"); 
    ImageView view = new ImageView(image); 
    popup.getChildren().add(view); 
    Scene dialogScene = new Scene(popup); 
    dialog.setScene(dialogScene); 
    dialog.show(); 

    Platform.runLater(new Runnable() { 

     @Override 
     public void run() { 

      Match msg = stablishSearchConditions(); 
      TreeItem<String> root = new TreeItem<>("ROOT"); 
      int indexName = 1; 
      String mensaje = "Mensaje "; 
      treeLabelResults.setText(""); 
      arbol.setRoot(root); 

      for (Match message : msg.each()) { 

       TreeItem<String> nodo = new TreeItem<String>(mensaje + indexName); 
       root.getChildren().add(nodo); 
       root.setExpanded(true); 
       String mens = message.getMessage(); 
       TreeItem<String> nodo2 = new TreeItem<String>(mens); 
       nodo.getChildren().add(nodo2); 

       indexName++; 
      } 
      dialog.close(); 
     } 
    }); 

} 

回答

2

你被在此線程運行昂貴的操作阻塞FX應用程序線程。這可以防止更新您的用戶界面,包括動畫GIF。

移動而不是昂貴的操作非應用程序線程,並只使用Platform.runLater()「落實」的UI更新:

Runnable expensiveTask =() -> { 
    // expensive operations that should not run on the application thread 
    Match msg = stablishSearchConditions(); 
    TreeItem<String> root = new TreeItem<>("ROOT"); 
    int indexName = 1; 
    String mensaje = "Mensaje "; 

    for (Match message : msg.each()) { 
     TreeItem<String> nodo = new TreeItem<String>(mensaje + indexName); 
     root.getChildren().add(nodo); 
     root.setExpanded(true); 
     String mens = message.getMessage(); 
     TreeItem<String> nodo2 = new TreeItem<String>(mens); 
     nodo.getChildren().add(nodo2); 

     indexName++; 
    } 

    // update ui -> application thread 
    Platform.runLater(() -> { 
     treeLabelResults.setText(""); 
     arbol.setRoot(root); 
     dialog.close(); 
    }); 
}; 

// start new thread for expensiveTask 
new Thread(expensiveTask).start(); 
+0

非常感謝你!它像一個魅力工作! – master89