2016-05-23 81 views
4

我想创建一个javafx输入对话框,我把它放在一个任务中,但是我的代码没有创建任何对话框。在JavaFX任务中创建一个JavaFX对话框

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

调试,它是Task类

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

如果我不把它导致应用程序挂起里面的任务对话框的下面捕获方法中去。

+0

你知道,'showAndWait'不会阻止JavaFX应用程序线程,因此并不需要在不同的线程上运行(其实**不应该在非fx应用程序不同的线程上运行**)? – fabian

+0

我不确定你为什么这样做。您提供的示例任务除了显示对话框外没有其他任何操作,可以在没有任务的情况下在JavaFX应用程序线程上完成。所以我真的不知道你在做什么,也许你正在试图做到这一点:[JavaFX2:我可以暂停后台任务/服务?](http://stackoverflow.com/questions/14941084/javafx2- can-i-pause-a-background-task-service) – jewelsea

+0

是的,我需要保持主线程以获取来自用户的输入,我尝试使用链接中提供的未来任务概念,但它会停止应用程序并使反应迟钝。 – bhavesh

回答

2

您必须确保对话框在JavaFX Application Thread上打开,因为每次GUI更新都必须在JavaFX的此线程上发生。

可以实现它想:

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