在文本字段中选择一个单词

发布于 2025-01-05 22:21:01 字数 1232 浏览 4 评论 0原文

以下代码应该在 JavaFX TextField 中选择一个单词:

public class NewFXMain extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        final TextInputControl textField = new TextField("Hello World, World!");

        Button button = new Button("select");
        button.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                textField.positionCaret(0);
                textField.selectNextWord();

                System.out.println(textField.getSelectedText());
            }
        });

        VBox root = new VBox();
        root.getChildren().add(textField);
        root.getChildren().add(button);
        primaryStage.setScene(new Scene(root, 300, 100));
        primaryStage.show();
    }
}

它在控制台中打印 Hello,但在界面中没有选择任何内容(突出显示)。 如果对 TextArea 执行相同的操作,则文本被正确选择。

TextField 的(错误)结果:

在此处输入图像描述

以及(正确)结果带有 TextArea

在此处输入图像描述

发生了什么事?!?

The following code is supposed to select a word in a JavaFX TextField:

public class NewFXMain extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        final TextInputControl textField = new TextField("Hello World, World!");

        Button button = new Button("select");
        button.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                textField.positionCaret(0);
                textField.selectNextWord();

                System.out.println(textField.getSelectedText());
            }
        });

        VBox root = new VBox();
        root.getChildren().add(textField);
        root.getChildren().add(button);
        primaryStage.setScene(new Scene(root, 300, 100));
        primaryStage.show();
    }
}

It prints Hello in the console, however in the interface nothing is selected (highlighted).
If one does the same with a TextArea, the text is correctly selected.

The (wrong) result with a TextField:

enter image description here

And the (correct) result with a TextArea:

enter image description here

What's going on?!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一直都在从未离去 2025-01-12 22:21:01

TextField 除非有焦点,否则不会显示选择(尽管我不确定它是错误还是功能)。您可以使用以下代码查看选择:

    button.setOnAction(new EventHandler<ActionEvent>() {

        public void handle(ActionEvent t) {
            textField.requestFocus(); // get focus first
            textField.positionCaret(0);
            textField.selectNextWord();

            System.out.println(textField.getSelectedText());
        }
    });

TextField doesn't show selection unless it has focus (although I'm not sure is it a bug or a feature). You can see selection by using next code:

    button.setOnAction(new EventHandler<ActionEvent>() {

        public void handle(ActionEvent t) {
            textField.requestFocus(); // get focus first
            textField.positionCaret(0);
            textField.selectNextWord();

            System.out.println(textField.getSelectedText());
        }
    });
手心的海 2025-01-12 22:21:01

问题的答案是,我们对本机控件在这方面的理解是,它们在文本字段失去焦点时清除选择,并且在获得焦点时(通常)选择所有文本。我们在 JavaFX 中使用 UI 控件的目的是拥有原生的感觉和定制的外观。当然,我们可以改变其工作方式的想法,但它必须与保持原生感觉的目标相平衡(包括在获得焦点时进行全选,并且在失去焦点时不显示选择,即使存在选择。但是,如果只是选择获得焦点的全选,那么失去焦点的任何选择就没有什么意义,并且由于奇怪的边缘情况仍然存在,因此我们必须想知道是否值得改变这一点)。

The answer to the question is that our understanding of native controls in this regard is that they both clear selection when the text field loses focus and that they (typically) select all text when gaining focus. Our intent with UI controls in JavaFX was to have a native feel but a customized look. Of course, we can have our mind changed on how this works, but it has to be balanced with the goal of maintaining a native feel (which includes select-all on focus gained and not showing selection on focus lost, even if selection exists. But since there is little point to there being any selection on focus lost if it is just going to select-all on focus gained and since weird edge cases will still exist as a result, we have to wonder whether it is worth changing this).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文