不幸的是,Spinner的表现不符合预期:在大多数操作系统中,它应该将编辑后的值提交给失去的焦点。更不幸的是,它没有提供任何配置选项来轻松使其表现出预期的效果。
因此,我们必须手动将侦听器中的值提交给focusedProperty。从好的方面来说,Spinner已经有这样做的代码-
它是私有的,但是,我们必须对其进行c&p
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 prespinner.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中也不起作用。可能是某些内部侦听器丢失或微调器尚未更新到相对较新的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(这是未记录的行为!)
回到微调器:我们可以使用格式化程序值的这种commit-on-focusLost行为来强制对spinnerFactory的值进行提交。就像是
// normal setup of spinnerSpinnerValueFactory factory = new IntegerSpinnerValueFactory(0, 10000, 0);spinner.setValueFactory(factory);spinner.setEditable(true);// hook in a formatter with the same properties as the factoryTextFormatter formatter = new TextFormatter(factory.getConverter(), factory.getValue());spinner.getEditor().setTextFormatter(formatter);// bidi-bind the valuesfactory.valueProperty().bindBidirectional(formatter.valueProperty());
需要注意的是编辑(无论是打字或以编程方式替换/追加/粘贴文本),并 没有 引发提交-所以,如果提交上文本变化需要此不能使用。



