java - JavaFX 在打开新的 TextInputDialog 时取消选择文本
问题描述
我以为我找到了答案,deselect()
但奇怪的是它什么也没做,打开时文本仍然全部选中。
TextInputDialog textInput = new TextInputDialog("whatever text");
textInput.initOwner(sentence.stage);
Optional<String> result = textInput.showAndWait();
if (result.isPresent()) {
// process
}
textInput.getEditor().deselect();
解决方案
该Dialog#showAndWait()
方法在对话框关闭之前不会返回。这意味着您的呼叫deselect()
为时已晚。但是,简单地重新排序您的代码似乎并不能解决您的问题。这看起来像是一个时间问题;当字段获得焦点时,可能会选择文本,因此您需要在此之后取消选择文本。例如:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
var button = new Button("Show dialog...");
button.setOnAction(
ae -> {
var textInput = new TextInputDialog("Some text");
textInput.initOwner(primaryStage);
Platform.runLater(textInput.getEditor()::deselect);
textInput.showAndWait();
});
var root = new StackPane(button);
primaryStage.setScene(new Scene(root, 500, 300));
primaryStage.show();
}
}
在显示对话框和选择文本之后执行Runnable
传递给。runLater
推荐阅读
- module - systemverilog 模块命名空间
- flutter - 在 Flutter 中使用 Firebase/Google 登录时无法注销并重定向到 LoginPage
- php - 消息输入字段验证
- mysql - 从 UML 到 ER 图的抽象类。可能的 ?如何?
- java - 如何在@ManyToMany JPA 关系的一侧反转键值映射类型
- python - 如何在 QWebEngineView 中指定用户代理
- pybind11 - 如何使用 pybind11 绑定两个嵌套结构
- winapi - FileCreate 的一些问题
- bash - 如何编写杀死 docker 容器的脚本?
- javascript - 如何将 Webview 设置为与 Chrome 浏览器完全一样?