Example usage for javafx.concurrent Task stateProperty

List of usage examples for javafx.concurrent Task stateProperty

Introduction

In this page you can find the example usage for javafx.concurrent Task stateProperty.

Prototype

@Override
    public final ReadOnlyObjectProperty<State> stateProperty() 

Source Link

Usage

From source file:employees.Employees.java

@Override
public void start(Stage stage) throws Exception {
    //        Parent root = FXMLLoader.load(getClass().getResource("MainUI.fxml"));
    //        /* w  w  w.  j a va 2  s  .  c o m*/
    //        Scene scene = new Scene(root);
    //        
    //        stage.setScene(scene);
    //        stage.show();
    //new LoginStage();
    Task<String> inittask = inittasks.pingServer();
    StringProperty pingResponse = new SimpleStringProperty();
    StringProperty messages = new SimpleStringProperty();
    pingResponse.bind(inittask.valueProperty());
    messages.bind(inittask.messageProperty());
    inittask.stateProperty().addListener((workerState, oldState, newState) -> {
        if (newState == Worker.State.SUCCEEDED) {
            String response = pingResponse.get();
            JSONObject obj = new JSONObject(response);
            String Status = obj.getString("Status");
            if (Status.equals("OK")) {
                AlertDialog ad = new AlertDialog();
                ad.showDialog(AlertTypes.Types.INFORMATION, "Successfull", "PING OK", "Connection Successfull");
            }
        } else if (newState == Worker.State.CANCELLED) {
            AlertDialog ad = new AlertDialog();
            ad.showDialog(AlertTypes.Types.WARNING, "Operation Aborted", "Operation Aborted",
                    "Task Was Cancelled,The System Will Now Exit");
            System.exit(0);
        } else if (newState == Worker.State.FAILED) {
            StringBuilder errstr = new StringBuilder();
            errstr.append(
                    "An Error Has Occured While Connecting to The Server, A Description of the Error is Shown Below : \n");
            errstr.append(messages.toString());
            AlertDialog ad = new AlertDialog();
            ad.showDialog(AlertTypes.Types.ERROR, "An Error Occurred While Connecting to The Server", "Error",
                    messages.get());
            Optional<ButtonType> response = AlertDialog.showConfirmation(
                    "Unable to Connect to The Server: Would you Like To Review Your Network Settings?",
                    "Connection Settings Review", "Review Connection Settings?");
            if (response.get() == ButtonType.OK) {
                try {
                    new initSettingsStage();
                } catch (IOException ex) {
                    Logger.getLogger(Employees.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    });
    new Thread(inittask).start();

}

From source file:utilitybasedfx.MainGUIController.java

private void enableWorking(Task task) {
    btnImportSource.setDisable(true);/* w  w  w  .  j  ava 2 s .  co m*/
    btnImportCompiled.setDisable(true);
    btnCompileFromSource.setDisable(true);
    btnGenerate.setDisable(true);

    lblWorking.setVisible(true);
    barWorking.setVisible(true);
    lblWorkingTask.setVisible(true);

    lblWorkingTask.textProperty().bind(task.messageProperty());
    barWorking.progressProperty().bind(task.progressProperty());

    task.stateProperty().addListener(new ChangeListener<Worker.State>() {
        @Override
        public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State oldState,
                Worker.State newState) {
            if (newState == Worker.State.SUCCEEDED) {
                disableWorking();
            } else if (newState == Worker.State.FAILED) {
                disableWorking();

                //Utils.stackErrorDialog((Exception)task.getException());
                task.getException().printStackTrace();

                Alert alert = new Alert(AlertType.ERROR);
                alert.setTitle("There was an error");
                alert.setHeaderText("There was some kind of error while trying to do the current task");
                alert.setContentText(task.getException().toString());

                alert.showAndWait();
            }
        }
    });

    new Thread(task).start();
}