2016-09-03 2 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(); 

문제

는 changind window.showAndWait(); 단지 window.show()을 통해 해결 될 수 있지만, 그것은 또한 다른 방법으로 해결 될 수 있을지 궁금하네요.

+0

이 도움말이 있습니까? (http://stackoverflow.com/questions/26022699/javafx-text-fields-are-not-updating-on-gui) –

+2

제공 한 코드가 잘 작동합니다. 사용중인 Java 버전은 무엇입니까? JavaFX 스레드에 위의 코드? 앱을 지연시키는 Lock 또는 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

관련 문제