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