Spinner<Integer> bind to IntegerProperty

Viewed 3375

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.

2 Answers

Ok it is late but let me propose something else for the others. You can simply call the asObject() on your 'integerProperty'. That will send you back ObjectProperty value.

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

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

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