2016-09-03 8 views
2

基本的には、テキストフィールドを持つポップアップディスプレイがあります。ユーザーが名前を入力する必要があります(名前を取得して後で使用しますが、問題はありません)。残念ながら、テキストフィールドのテキストは更新されません(図形的に、プログラムはtf.getText()によって型指定されたものを取得できますが、テキストの更新は見えません)。JavaFX TextFieldはテキストを更新しません。

Stage window = new Stage(); 
    window.initModality(Modality.APPLICATION_MODAL); 
    window.setTitle("Naming"); 
    window.setMinWidth(300); 
    window.setMinHeight(200); 
    Label label = new Label(); 
    label.setText("Please type a name"); 
    Button submitButton = new Button("Submit"); 
    TextField tf = new TextField(); 
    tf.setText("Please enter a name"); 
    tf.setMaxWidth(200); 
    submitButton.setOnAction(e ->{ 
     System.out.println(tf.getText()); 
     window.close(); 
    }); 
    VBox layout = new VBox(10); 
    layout.getChildren().addAll(label, submitButton, tf); 
    Scene scene = new Scene(layout); 
    window.setScene(scene); 
    window.showAndWait(); 

問題はちょうどwindow.show()にchangind window.showAndWait();を介して固定することができ、それはまた、他の方法で解決することができる場合、私は思ったんだけど。

+0

このヘルプはありますか? (http://stackoverflow.com/questions/26022699/javafx-text-fields-are-not-updating-on-gui) –

+2

あなたが提供したコードはうまく機能しています。あなたが使っているJavaのバージョンは? JavaFXスレッドに上記のコード?あなたは、アプリケーションを遅らせるロックまたはThread.sleep()メソッドを実行していますか? – GOXR3PLUS

答えて

0

showAndwait()メソッドはブロッキングコールです(待機しているため)。メインイベントループからこのコマンドを実行して、ウィンドウを再ペイントするなどのイベントの処理をブロックします。 show()は続行を許可し、したがってこの処理を可能にします。ここにあることは、あなたが入っているスコープで待つのではなく、元のイベントループをブロックしてダイアログを待たせることです。これは、別のシーン(メインイベントループを持つメインシーン)でこのダイアログを起動することで実行できます。これを示すコードの例を次に示します。

public class Main extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     try { 
      // Your snippet 
      Stage window = new Stage(); 
      window.initModality(Modality.APPLICATION_MODAL); 
      window.setTitle("Naming"); 
      window.setMinWidth(300); 
      window.setMinHeight(200); 
      Label label = new Label(); 
      label.setText("Please type a name"); 
      Button submitButton = new Button("Submit"); 
      TextField tf = new TextField(); 
      tf.setText("Please enter a name"); 
      tf.setMaxWidth(200); 
      submitButton.setOnAction(e ->{ 
       System.out.println(tf.getText()); 
       window.close(); 
      }); 
      VBox layout = new VBox(10); 
      layout.getChildren().addAll(label, submitButton, tf); 
      Scene scene2 = new Scene(layout); 
      window.setScene(scene2); 

      // Root window 
      VBox layout2 = new VBox(); 
      Scene scene = new Scene(layout2,400,400); 
      Button openButton = new Button("Open"); 
      openButton.setOnAction(e -> { 
       // Launch from action handling thread 
       window.showAndWait(); 

       // Add any logic here like acquiring the entered data from 
       // the window and do interesting stuff with it 
      }); 
      layout2.getChildren().add(openButton); 
      primaryStage.setScene(scene); 

      // Launch main window with non-blocking call 
      primaryStage.show(); 


     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+1

* "showAndWait()メソッドはブロッキング呼び出しです(待機しているため)。GUIスレッドからこのコマンドを実行しているため、ウィンドウを再描画できません。実際には 'showAndWait'はブロックされますが、UIを更新するネストされたイベントループに入ります。 BTW:* "Action Handling thread" * **は** GUIスレッドです。 – fabian

+0

これは間違いなく、私がスレッドについて話していたとき、私はJavaFXの場合のイベントループについて実際に話していました。結果的に私は編集します、ありがとう。 – Memophysic

関連する問題