javafx - display dialog and make user wait for met

2019-08-22 00:43发布

In a JavaFX application a custom method "retrain" takes a long time when given large input. I want to display a dialog and make the user wait for the method to end. The code here allows the user to close the dialog which I would rather them not be able to. When I take away the cancel button, the dialog won't close at all due to the implementation of Dialog in java. If anyone could point me in the right direction I would really appreciate it.

@FXML
    private void handleMarkovText() {
        textgen.MarkovTextGenerator mtg = mainApp.getMTG();
        Task<textgen.MarkovTextGenerator> task = new Task<textgen.MarkovTextGenerator>() {
            @Override
            public textgen.MarkovTextGenerator call() {
                // process long-running computation, data retrieval, etc...

            mtg.retrain(textBox.getText());
            return mtg;
        }
    };

    Dialog<Void> loadDialog = new Dialog<Void>();
        loadDialog.setOnCloseRequest( e -> {

            if(!task.isDone()) {
                e.consume();
            }

        });

    loadDialog.setContentText("Training MTG...");

    task.setOnRunning( e -> {
        ButtonType cancelButtonType = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);
        //Dialog<Void> loadDialog = new Dialog<Void>();
        loadDialog.getDialogPane().getButtonTypes().add(cancelButtonType);
        loadDialog.show();
        if(task.isCancelled()) {
            System.out.println("Cancelling on running");
        }
    });


    task.setOnSucceeded(e -> {
        System.out.println("Succeeded");
        loadDialog.close();
        textgen.MarkovTextGenerator result = task.getValue();
        mainApp.showMarkovDialog(result);
        // update UI with result
    });

    task.setOnCancelled(e -> {
        System.out.println("Cancelled");
    });

    task.setOnFailed(e -> {
        //System.out.println("failed");
    });
    //ProgressBar bar = new ProgressBar();
    //bar.progressProperty().bind(task.progressProperty());
   Thread thread  = new Thread(task);
   thread.start();





    // train/retrain markov
    // show results

}

0条回答
登录 后发表回答