Warm tip: This article is reproduced from serverfault.com, please click

java-在JavaFX Spinner中手动输入文本不会更新值(除非用户按ENTER)

(java - Manually typing in text in JavaFX Spinner is not updating the value (unless user presses ENTER))

发布于 2015-09-01 20:38:27

在用户明确按下Enter键之前,似乎Spinner控件不会更新手动键入的值。因此,他们可以键入一个值(不按Enter键)以退出控件,然后提交表单,并且 Spinner 中显示的值不是 Spinner 的值,而是旧值。

我的想法是向丢失的焦点事件添加一个侦听器,但是我看不到获得对输入值的访问的方法吗?

spinner.focusedProperty().addListener((observable, oldValue, newValue) -> 
{
    //if focus lost
    if(!newValue)
    {
        //somehow get the text the user typed in?
    }
});

这是奇怪的行为,似乎违反了GUI微调控件的约定。

Questioner
James Wierzba
Viewed
0
2017-05-23 20:18:11

不幸的是,Spinner的表现不符合预期:在大多数操作系统中,它应该将编辑后的值提交给失去的焦点。更不幸的是,它没有提供任何配置选项来轻松使其表现出预期的效果。

因此,我们必须手动将侦听器中的值提交给focusedProperty。从好的方面来说,Spinner已经有这样做的代码-它是私有的,但是我们必须对它进行c&p

/**
 * c&p from Spinner
 */
private <T> void commitEditorText(Spinner<T> spinner) {
    if (!spinner.isEditable()) return;
    String text = spinner.getEditor().getText();
    SpinnerValueFactory<T> valueFactory = spinner.getValueFactory();
    if (valueFactory != null) {
        StringConverter<T> converter = valueFactory.getConverter();
        if (converter != null) {
            T value = converter.fromString(text);
            valueFactory.setValue(value);
        }
    }
}

// useage in client code
spinner.focusedProperty().addListener((s, ov, nv) -> {
    if (nv) return;
    //intuitive method on textField, has no effect, though
    //spinner.getEditor().commitValue(); 
    commitEditorText(spinner);
});

请注意,有一种方法

textField.commitValue()

我本来希望...好吧...提交该值,但没有任何效果。它已实现(最终!),以更新textFormatter的值(如果有)。即使你使用textFormatter进行验证,在Spinner中也不起作用可能是某些内部侦听器丢失或 Spinner 尚未更新到相对较新的api-但是没有挖掘。


更新

在使用TextFormatter进行更多操作时,我注意到格式化程序可以保证对focusLost进行提交

当控件失去焦点或被提交时,将更新该值(仅TextField)

确实如文档所述工作,以便我们可以在格式化程序的valueProperty中添加一个侦听器,以便在提交值时得到通知:

TextField field = new TextField();
TextFormatter fieldFormatter = new TextFormatter(
      TextFormatter.IDENTITY_STRING_CONVERTER, "initial");
field.setTextFormatter(fieldFormatter);
fieldFormatter.valueProperty().addListener((s, ov, nv) -> {
    // do stuff that needs to be done on commit
} );

触发提交:

  • 用户点击ENTER
  • 控制失去重点
  • 以编程方式调用field.setText(这是未记录的行为!)

回到 Spinner :我们可以使用格式化程序值的这种commit-on-focusLost行为来强制对spinnerFactory的值进行提交。就像是

// normal setup of spinner
SpinnerValueFactory factory = new IntegerSpinnerValueFactory(0, 10000, 0);
spinner.setValueFactory(factory);
spinner.setEditable(true);
// hook in a formatter with the same properties as the factory
TextFormatter formatter = new TextFormatter(factory.getConverter(), factory.getValue());
spinner.getEditor().setTextFormatter(formatter);
// bidi-bind the values
factory.valueProperty().bindBidirectional(formatter.valueProperty());

需要注意的是编辑(无论是打字或以编程方式替换/追加/粘贴文本),并没有引发提交-所以,如果提交上文本变化需要此不能使用。