Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner<Integer> bind to IntegerProperty

Tags:

java

javafx-8

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.

like image 877
SeeMoreGain Avatar asked Mar 07 '16 03:03

SeeMoreGain


1 Answers

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.

like image 100
VGR Avatar answered Nov 15 '22 01:11

VGR