4

I am trying to create a javafx input dialogue box, I am putting it inside a task but my code is not creating any dialogue box.

                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:");

                // Traditional way to get the response value.
                Optional<String> result = dialog.showAndWait();
                if (result.isPresent()){
                    System.out.println("Your name: " + result.get());
                }

                // The Java 8 way to get the response value (with lambda expression).
                result.ifPresent(name -> System.out.println("Your name: " + name));
                return null;
                    }
                };
            Thread pt = new Thread(passwordBox);
            pt.start();

on debugging, it is going inside the following catch method of Task class

catch (final Throwable th) {
                // Be sure to set the state after setting the cause of failure
                // so that developers handling the state change events have a
                // throwable to inspect when they get the FAILED state. Note
                // that the other way around is not important -- when a developer
                // observes the causeOfFailure is set to a non-null value, even
                // though the state has not yet been updated, he can infer that
                // it will be FAILED because it can be nothing other than FAILED
                // in that circumstance.
                task.runLater(() -> {
                    task._setException(th);
                    task.setState(State.FAILED);
                });
                // Some error occurred during the call (it might be
                // an exception (either runtime or checked), or it might
                // be an error. In any case, we capture the throwable,
                // record it as the causeOfFailure, and then rethrow. However
                // since the Callable interface requires that we throw an
                // Exception (not Throwable), we have to wrap the exception
                // if it is not already one.
                if (th instanceof Exception) {
                    throw (Exception) th;
                } else {
                    throw new Exception(th);
                }

If I am not putting the dialogue box inside the task it is causing application to hang.

DVarga
  • 21,311
  • 6
  • 55
  • 60
Yash
  • 347
  • 2
  • 18
  • You do know that `showAndWait` does not block the javafx application thread and thus does not need to be run on a different thread (in fact it **should not be run** on a non-fx-application different thread)? – fabian May 23 '16 at 14:46
  • I'm not sure why you do this. The sample task you provide isn't doing anything other than showing a dialog, which could be done on the JavaFX application thread without a Task. So I don't really know what you are trying to achieve, perhaps you are trying to do this: [JavaFX2: Can I pause a background Task / Service?](http://stackoverflow.com/questions/14941084/javafx2-can-i-pause-a-background-task-service) – jewelsea May 23 '16 at 17:16
  • yes i need to hold the main thread to get the input from the user, I tried using the future task concept provided in the link but it stops the app and make in unresponsive. – Yash May 24 '16 at 10:55

1 Answers1

2

You have to ensure that the dialog is opened on the JavaFX Application Thread, as every GUI update must happen on this thread in JavaFX.

You can achieve it like:

Task<Void> passwordBox = new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        TextInputDialog dialog = new TextInputDialog("walter");
                        dialog.setTitle("Text Input Dialog");
                        dialog.setHeaderText("Look, a Text Input Dialog");
                        dialog.setContentText("Please enter your name:");

                        // Traditional way to get the response value.
                        Optional<String> result = dialog.showAndWait();
                        if (result.isPresent()){
                            System.out.println("Your name: " + result.get());
                        }

                        // The Java 8 way to get the response value (with lambda expression).
                        result.ifPresent(name -> System.out.println("Your name: " + name));

                    }
                });

                return null;
            }
        };
DVarga
  • 21,311
  • 6
  • 55
  • 60