Converting Integer to ObservableValue<Integer> in javafx
Asked Answered
N

4

19

How to convert Integer To ObservableValue<Integer> in javafx 2.0 and later ?

Nakada answered 19/1, 2013 at 9:44 Comment(0)
N
42

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();
Nakada answered 19/1, 2013 at 10:26 Comment(0)
S
11

Another way.

new SimpleIntegerProperty(integer_value).asObject()
Stowage answered 23/9, 2014 at 5:46 Comment(2)
#asObject() was added in JavaFX 8Pawnshop
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 SimpleIntegerPropertyDeanery
M
7

if you use tableview do this : just change Integer to Number

@FXML
private TableColumn<Sockets,Number> key;
...
key.setCellValueFactory(cellData -> cellData.getValue().socketIdProperty());
Massingill answered 18/6, 2019 at 20:33 Comment(1)
Same for Double values.Op
F
4

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;
Freeload answered 5/7, 2016 at 8:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.