Reputation: 408
I am creating an application that have different classes such as User
, Product
, Donor
and Car
. Each of these classes stores relevant information to be shown in a JavaFX TableView
– the User
class having a String name
and int userID
for instance.
I collect all of these in a class called Data
that takes the above mentioned classes and stores them. I end up with an ArrayList<Data>
where each index stores the four classes above.
If I make an ObservableArrayList
based on the ArrayList<Data>
how do I retrieve the information from each class stored in the Data
class? I am following Oracle's own tutorial here, but it only accounts for a case where you want to store a single class' information.
Thanks!
Upvotes: 1
Views: 2511
Reputation: 159376
The tutorial you mention uses a PropertyValueFactory. That is not applicable here as that only introspects on a given class, but if you look at the documentation for that class it includes the following long form:
TableColumn<Person,String> firstNameCol = new TableColumn<Person,String>("First Name");
firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<Person, String> p) {
// p.getValue() returns the Person instance for a particular TableView row
return p.getValue().firstNameProperty();
}
});
}
So you can use the same concept with your data class to retrieve the appropriate field from the user object of the data object:
firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<Data, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<Data, String> data) {
// data.getValue() returns the Data instance for a particular TableView row
return data.getValue().getUser().firstNameProperty();
}
});
}
Some of the ugliness can be removed if you use Java 8 lambdas.
If your user object doesn't have property accessors for the values, but only getters, you can wrap the result in a ReadOnlyObjectWrapper as described in the PropertyValueFactory documentation:
return new ReadOnlyObjectWrapper(data.getValue().getUser().getFirstName());
This is fine if your table is not editable and you don't want the table data to change automatically If the underlying field changes, otherwise the property accessor based method is preferred.
Upvotes: 1