How to convert Integer
To ObservableValue<Integer>
in javafx 2.0 and later ?
Converting Integer to ObservableValue<Integer> in javafx
We use a ReadOnlyObjectWrapper<>(*integer value*);
and store the value in a ObservableValue<Integer>
reference.
ObservableValue<Integer> obsInt = new ReadOnlyObjectWrapper<>(intValue);
Update
Starting JavaFX 8, you can also do the following :
ObservableValue<Integer> obsInt = new SimpleIntegerProperty(intValue).asObject();
Another way.
new SimpleIntegerProperty(integer_value).asObject()
One subtle issue, if you need the
Integer
(object) as opposed to the primitive int
(pehaps to allow null references), you have to use the ReadOnlyObjectWrapper<Integer>
and not the SimpleIntegerProperty
–
Deanery if you use tableview do this : just change Integer to Number
@FXML
private TableColumn<Sockets,Number> key;
...
key.setCellValueFactory(cellData -> cellData.getValue().socketIdProperty());
Same for Double values. –
Op
IntegerProperty
implements ObservableValue<Number>
not ObservableValue<Integer>
. So you should do:
// Here Person is a class and age is a variable of type IntegerProperty
ObservableValue<Number> ob = Person.age;
© 2022 - 2024 — McMap. All rights reserved.
#asObject()
was added in JavaFX 8 – Pawnshop