2016-05-23 5 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-缶I-ポーズ - バックグラウンドタスク・サービス) – jewelsea

+0

はい私は、ユーザからの入力を取得するためにメインスレッドを保持する必要がある、私がリンクに提供する将来のタスクの概念を使用してみましたが、それはアプリを停止し、中に作ります応答しない。 – bhavesh

答えて

2

あなたはすべてGUIのアップデートはJavaFXの中に、このスレッド上で発生しなければならないとダイアログは、JavaFX Application Thread上で開かれていることを確認する必要があります。

あなたはそれが好きで達成することができます:

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; 
      } 
     }; 
関連する問題