ItachiUchiha
ItachiUchiha

Reputation: 36782

Converting Integer to ObservableValue<Integer> in javafx

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

Upvotes: 20

Views: 31888

Answers (4)

Oumalek Mohamed
Oumalek Mohamed

Reputation: 96

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

@FXML
private TableColumn<Sockets,Number> key;
...
key.setCellValueFactory(cellData -> cellData.getValue().socketIdProperty());

Upvotes: 8

Pramod Kumar Sharma
Pramod Kumar Sharma

Reputation: 609

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;

Upvotes: 4

ItachiUchiha
ItachiUchiha

Reputation: 36782

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();

Upvotes: 43

Andrey Morozov
Andrey Morozov

Reputation: 7979

Another way.

new SimpleIntegerProperty(integer_value).asObject()

Upvotes: 11

Related Questions