I have a list of objects which containing lists of a complex data type and I need to bind these properties to my TableView. My entities are looking like this:
public class Customer
{
StringProperty property1;
StringProperty property2;
....
List<Contract> contracts;
}
public class Contract
{
StringProperty property1;
StringProperty property2;
....
}
My observableArrayList contains a list of Customer which contains a list of Contract. Getting the Customer properties is easy:
sampleColumn1.setCellValueFactory(celldata -> celldata.getValue().sampleProperty1());
sampleColumn2.setCellValueFactory(celldata -> celldata.getValue().sampleProperty2());
...
But I can't get the properties of the Contract list.. Currently I do the following workaround:
PropertyValueFactory<Customer, List<Contract>> contractFactory = new PropertyValueFactory<>("contracts");
contractIdColumn.setCellValueFactory(contractFactory);
contractIdColumn.setCellFactory(celldata -> new TableCell<Customer>, List<Contract>>(){
@Override
public void updateItem(List<Contract> contracts, boolean empty){
super.updateItem(contracts, empty);
if(empty)
setText("");
else
setText(contracts.stream().map(Contract::getContractId)
.collect(Collectors.joining("\n")));
}
});
But this code is dirty, does somebody know an approach to do this with cellValueFactory and lambda expressions? Any suggestions?