I am trying to create a javafx input dialog, I put it in a task, but my code does not create any dialog.
Task<Void> passwordBox = new Task<Void>() {
@Override
protected Void call() throws Exception {
TextInputDialog dialog = new TextInputDialog("walter");
dialog.setTitle("Text Input Dialog");
dialog.setHeaderText("Look, a Text Input Dialog");
dialog.setContentText("Please enter your name:");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
System.out.println("Your name: " + result.get());
}
result.ifPresent(name -> System.out.println("Your name: " + name));
return null;
}
};
Thread pt = new Thread(passwordBox);
pt.start();
when debugging, it enters the following catch method of the Task class
catch (final Throwable th) {
task.runLater(() -> {
task._setException(th);
task.setState(State.FAILED);
});
if (th instanceof Exception) {
throw (Exception) th;
} else {
throw new Exception(th);
}
If I do not place the dialog box inside the task, it causes the application to freeze.
source
share