How to keep progress indicator running in javafx

I want a progress indicator to stop only after completely updating the ui. But it stops after executing last line of code in ui method. The indicator should work as:

1> Start the progress
2> Run the progress until all nodes are added to the scene
3> Stop after completion

The way i update is, i pass BorderPane to the thread and set its center a gridpane, which is the last line of code after which the indicator stops. And the loginTask, if i start it inside Application Thread the indicator does not spin !

borderpane.setCenter(gpane);

UI method

{
Loading loadBar = new Loading(stage);
Task<Boolean> loginTask= checkCredTask();
loginTask.setOnSucceeded(value -> {
            loadBar.hideProgress(); });

loadBar.startTask(loginTask);
(new Thread(loginTask)).start();

}

Progress Bar

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);
    }
}

Task

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;
    }

load UI method

ExecutorService execsrv = Executors.newSingleThreadExecutor();
             execsrv.execute(new AdminUI(stage,pane,mb));
             execsrv.shutdown();


ANSWERS:


Your code won't even compile. Task<Boolean> loginTask does not override abstract call() method. Even if you wrap

ExecutorService execsrv = Executors.newSingleThreadExecutor();
execsrv.execute(new AdminUI(stage,pane,mb));
execsrv.shutdown();

in call() method it makes no sense, because you are executing new task inside another task.

Assumig that new AdminUI(stage,pane,mb) is long running process and return boolean you should do it in this way:

Task<Boolean> loginTask = new Task<Boolean>() {
    @Override
    protected Boolean call() throws Exception {
        // Start the UI 
        AdminUI ui = new AdminUI(stage, pane, mb);
        return ui.getBooleanValue(); 
    }
};

EDIT

As I said, you are executing AdminUI task inside loginTask what makes no sense. For now you are waiting only for login task but not for AdminUI task. Login task finishes faster than AdminUI that is why indicator stops to early. Extract AdminUI to another Task. This is pseudo code so you have to do it by your self.

Task<Boolean> loginTask= checkCredTask();
loginTask.setOnSucceeded(value -> {
    //****** THIS IS PSEUDO CODE ***********
    //start AdminUITask
    //AdminUITask.setOnSucceeded(value -> {
        //loadBar.hideProgress();
    //});
});


 MORE:


 ? Swift: Setting progress of NSProgressIndicator
 ? Swift 3 Conversion
 ? Swift 3 Conversion
 ? Swift 3 Conversion
 ? Long cycle blocks application
 ? What is equivalent of "dispatch_apply" in Swift 3?
 ? What is equivalent of "dispatch_apply" in Swift 3?
 ? What is equivalent of "dispatch_apply" in Swift 3?
 ? How to set edgesForExtendedLayout to none in Swift 3
 ? Cocoa - Application Freezes During Loading Data From XML