Spinner<Integer> bind to IntegerProperty

六月ゝ 毕业季﹏ 提交于 2019-12-08 19:34:12

问题


I want to use a FX8 Spinner control, but I want to bind the source to an IntegerProperty

int MIN = 0;
int MAX = 5000;
int STEP = 500;
IntegerProperty integerProperty = new SimpleIntegerProperty();

Spinner<Integer> spinner = new Spinner<>(MIN, MAX, STEP);

I understand the binding is set via binding to the valueProperty in the Value Factory. However this expects Property<Integer> and I cannot find a way to cast between IntegerProperty and Property<Integer>.

Obviously the below generates a compiler error:

spinner.getValueFactory().valueProperty().bindBidirectional(integerProperty);

Do I need to manually assign a change listener for both directions? Surely there is a neater solution using the valueProperty, this cannot have been an unforeseen situation.


回答1:


You can wrap an ObjectProperty:

ObjectProperty<Integer> objectProp = new SimpleObjectProperty<>(MIN);
IntegerProperty integerProperty = IntegerProperty.integerProperty(objectProp);

Spinner<Integer> spinner = new Spinner<>(MIN, MAX, STEP);

spinner.getValueFactory().valueProperty().bindBidirectional(objectProp);

IntegerProperty.integerProperty creates a property that's bidirectionally connected to the property it wraps.



来源:https://stackoverflow.com/questions/35835939/spinnerinteger-bind-to-integerproperty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!