【问题标题】:JavaFX TextField wont Update TextJavaFX TextField 不会更新文本
【发布时间】:2016-09-03 03:38:39
【问题描述】:

基本上,我有一个弹出式显示,其中有一个文本字段,我希望用户在其中输入一个名称(然后我最终得到该名称并稍后使用它,但这没关系)。不幸的是,文本字段中的文本没有更新(从图形上看,程序仍然可以获取通过 tf.getText() 输入的内容,但我看不到文本更新)。

    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.showAndWait(); 更改为window.show() 来解决,但我想知道它是否也可以通过其他方式解决。

【问题讨论】:

  • 您提供的代码运行良好,尽管需要信息。您使用什么 Java 版本?您是否将上述代码调用到 JavaFX 线程中?您是否正在运行任何使应用程序滞后的 Lock 或 Thread.sleep() 方法?

标签: java javafx textfield


【解决方案1】:

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

【讨论】:

  • showAndWait() 方法是一个阻塞调用(因为它会等待),并且您正在从 GUI 线程执行此命令,因此无法重新绘制窗口。” i> 这只是对了一半。确实showAndWait 会阻塞,但它会进入一个更新UI 的嵌套事件循环。顺便说一句:“动作处理线程” GUI 线程。
  • 没错,当我谈论线程时,我实际上是在谈论 JavaFX 的事件循环。我会编辑结果,谢谢。
猜你喜欢
  • 1970-01-01
  • 2019-09-13
  • 2017-04-21
  • 2014-09-02
  • 1970-01-01
  • 2018-04-12
  • 2018-06-18
  • 2018-07-13
  • 2016-09-10
相关资源
最近更新 更多