1
我想要一個進度指示器,只有在完全更新ui後才能停止。但是它在ui方法中執行最後一行代碼後停止。該指標應該工作爲:如何在javafx中保持進度指示器運行
1> Start the progress 2> Run the progress until all nodes are added to the scene 3> Stop after completion
的路上我更新,我通過BorderPane給線程,並設置其中心gridpane,這是代碼的指示燈停止之後的最後一行。 和loginTask,如果我在應用程序線程中啓動它,指示器不旋轉!
borderpane.setCenter(gpane);
UI方法
{
Loading loadBar = new Loading(stage);
Task<Boolean> loginTask= checkCredTask();
loginTask.setOnSucceeded(value -> {
loadBar.hideProgress(); });
loadBar.startTask(loginTask);
(new Thread(loginTask)).start();
}
進度條
public class Loading{
private static Stage stage;
private static ProgressIndicator p;
private static Alert alert;
public Loading(Stage s){
stage=s;
p=new ProgressIndicator();
alert = new Alert(AlertType.NONE);
}
public void startTask(Task<Boolean> cm){
if(p != null){
p.setProgress(-1);
p.progressProperty().unbind();
p.progressProperty().bind(cm.progressProperty());
alert.initOwner(stage);
alert.getDialogPane().setStyle("-fx-background-color:rgba(0,0,0,0);");
alert.getDialogPane().getScene().setFill(Color.TRANSPARENT);
alert.initStyle(StageStyle.TRANSPARENT);
alert.getDialogPane().setContent(p);
alert.show();
}
}
public void hideProgress(){
alert.setResult(ButtonType.CLOSE);
}
}
任務
private Task<Boolean> checkCredTask() {
Task<Boolean> loginTask = new Task<Boolean>() {
@Override
public Boolean call() {
Boolean result = false;
int flag = verifyCredential();
if (flag == 1) {
loadUI();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
result = true;
} else if (flag == 2) {
result = false;
}
return result;
}
};
return loginTask;
}
負載UI方法
ExecutorService execsrv = Executors.newSingleThreadExecutor();
execsrv.execute(new AdminUI(stage,pane,mb));
execsrv.shutdown();
更新的代碼,程序工作,問題是指示燈停止太早 – pebble
嘗試添加'了Thread.sleep(5000)'來loginTask,只是爲了測試。 – MBec
它確實有效,但當Thread.sleep執行指示器時暫停5000ms,然後繼續旋轉。有沒有辦法讓它始終旋轉 – pebble