在文本字段中选择一个单词
以下代码应该在 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
:
And the (correct) result with a TextArea
:
What's going on?!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TextField
除非有焦点,否则不会显示选择(尽管我不确定它是错误还是功能)。您可以使用以下代码查看选择: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:问题的答案是,我们对本机控件在这方面的理解是,它们在文本字段失去焦点时清除选择,并且在获得焦点时(通常)选择所有文本。我们在 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).