2016-05-10 71 views
0

我使用下面的代碼來顯示加載任務的進度指示器。我使用的是來自ControlsFX進度指示器的MaskerPane。 但是,當我使用的組件,maskerpane只顯示1次..請建議一個更好的方式來顯示進度指示器。javafx應用程序的進度指示

@FXML 
    private MaskerPane progressPane; 
@FXML 
     private void addMemberSelect() { 
      Task task = new Task<Void>() { 
       @Override 
       protected Void call() throws Exception { 
        progressPane.setVisible(true); 
        return null; 
       } 
       @Override 
       protected void succeeded(){ 
        super.succeeded(); 
        content.setContent(null); 
        ScreensController colllectScreenController = new ScreensController(); 
        colllectScreenController.loadScreen(Screens.ADD_MEMBER); 
        colllectScreenController.setScreen(Screens.ADD_MEMBER); 
        content.setContent(colllectScreenController); 
        progressPane.setVisible(false); 
       } 
      }; 
      new Thread(task).start(); 
     } 
+0

你的問題不是很清楚。你想從MaskPane中獲得什麼?什麼不起作用?你是什​​麼意思,「maskerpane只顯示一次」? – ItachiUchiha

+0

對不起,我的英語。其實我希望在切換場景時顯示進度。每個場景都包含一些數據庫操作。所以我想顯示進度指標。我使用的上述代碼只顯示一次進度 一次。顯示任何進度欄 – boycod3

+0

我沒有看到您使用[updateProgress()]更新進度(https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html#updateProgress-double -double-)在任務上。如果您不使用它,進度指示器將永遠不會顯示進度。有多個示例顯示如何在[Task](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html)的javadoc中使用它。 – ItachiUchiha

回答

0

這個例子可以幫助你。

public class MaskerPaneTest extends Application { 
@Override 
public void start(Stage primaryStage) throws Exception { 

    MaskerPane progressPane = new MaskerPane(); 
    progressPane.setVisible(false); 

    Button button = new Button("Show"); 
    button.setOnAction(event -> { 

      Task task = new Task<Void>() { 
       @Override 
       protected Void call() throws Exception { 
        progressPane.setVisible(true); 
        Thread.sleep(2000); 
        return null; 
       } 
       @Override 
       protected void succeeded(){ 
        super.succeeded(); 
        progressPane.setVisible(false); 
       } 
      }; 
      new Thread(task).start(); 
     }); 
    VBox box = new VBox(button, progressPane); 
    box.setAlignment(Pos.CENTER); 
    Scene scene = new Scene(box, 200, 200); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

} 來源:https://gist.github.com/TheItachiUchiha/13f8cc637872ebac4385f92cd16075a7

我測試了它和它的作品,所以你能適應它。