java 如何设置 JavaFX Spinner 的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38810846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the value of a JavaFX Spinner?
提问by Matt
I'm wondering how to set the value of a JavaFX Spinner, as I haven't been able to figure it out.
我想知道如何设置 JavaFX Spinner 的值,因为我一直无法弄清楚。
I know with Swing you can just use spinner#setValue but it seems to be different with JavaFX.
我知道使用 Swing 你可以只使用 spinner#setValue 但它似乎与 JavaFX 不同。
@FXML
private Spinner<Integer> spinner;
回答by James_D
spinner.getValueFactory().setValue(...);
回答by Fred Danna
Just in addition to James_D's answer, an extract of the JavaDoc of the value
attribute of the Spinner
class:
除了James_D的回答之外,value
还有Spinner
类属性的 JavaDoc 的摘录:
The value property on Spinner is a read-only property, as it is bound to the SpinnerValueFactory value property. Should the value factory change, this value property will be unbound from the old value factory and bound to the new one. If developers wish to modify the value property, they may do so with code in the ollowing form:
Object newValue = ...; spinner.getValueFactory().setValue(newValue);
Spinner 上的 value 属性是只读属性,因为它绑定到 SpinnerValueFactory 值属性。如果值工厂发生变化,该值属性将与旧值工厂解除绑定并绑定到新值工厂。如果开发人员希望修改 value 属性,他们可以使用以下形式的代码进行修改:
对象 newValue = ...; spinner.getValueFactory().setValue(newValue);