How to convert Integer
To ObservableValue<Integer>
in javafx 2.0 and later ?
Asked
Active
Viewed 3.1k times
17

ItachiUchiha
- 36,135
- 10
- 122
- 176
4 Answers
40
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();

ItachiUchiha
- 36,135
- 10
- 122
- 176
11
Another way.
new SimpleIntegerProperty(integer_value).asObject()

Andrey Morozov
- 7,839
- 5
- 53
- 75
-
`#asObject()` was added in JavaFX 8 – Paolo Fulgoni Jan 08 '16 at 10:58
-
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
` and not the `SimpleIntegerProperty` – Michael Landes Mar 04 '16 at 11:51
6
if you use tableview do this : just change Integer to Number
@FXML
private TableColumn<Sockets,Number> key;
...
key.setCellValueFactory(cellData -> cellData.getValue().socketIdProperty());

Oumalek Mohamed
- 76
- 1
- 4
3
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;

user7291698
- 1,972
- 2
- 15
- 30

Pramod Kumar Sharma
- 560
- 6
- 18