首页 > 解决方案 > 如何在输入时在微调器文本字段中选择数字或文本?

问题描述

我正在为整数使用 javafx 微调器。当我单击它或使用 TAB 移动到它时,有没有办法在 Spinner 中选择值?就像它适用于具有以下代码的 TextAreas 一样:

myTextArea.focusedProperty().addListener((obs, isFocused, isNowFocused) -> {
        if (isNowFocused) {
            myTextArea.selectAll();
   }
})

selectAll() 不适用于微调器。我想在不删除初始值的情况下开始输入微调器字段。

标签: javafxspinner

解决方案


您可以selectAll()调用TextField.Spinner

spinner.focusedProperty().addListener((obs, wasFocused, isFocused) -> {
    if (isFocused) {
        Platform.runLater(spinner.getEditor()::selectAll);
    }
});

尝试此操作时,我必须将呼叫包装在selectAll呼叫中Platform.runLater。当Spinner它获得干扰调用的焦点时,它本身可能正在做某事selectAll;usingPlatform.runLater将调用推迟到Spinner执行任何操作之后的某个时间。


推荐阅读