Binding nested object properties to TableView in JavaFx

java, javafx

Solution

I had the same problem and couldn't make it work in any suggested way, but what did the trick for me, if we instantiate columns and have nested property, for instance, in class Employee:

@FXML
private TableColumn<Employee, String> columnEmpName;
@FXML
private TableColumn<Employee, String> columnEmpLastName;
@FXML
private TableColumn<Employee, String> columnEmpJobDesc;

And class is:

public class Employee {
   private String name;
   private String lastName;
   .
   .
   private EmployeeJobDescription jobDesc; //this one is nested
}

I have made `ObservableValue<String>` out of string I picked up for that property

columnEmpName.setCellValueFactory(new PropertyValueFactory<Employee, String>("name"));
columnEmpLastName.setCellValueFactory(new PropertyValueFactory<Employee, String>("lastname"));
columnEmpJobDesc.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getJobDesc().getJobDescription()));

And the EmployeeJobDescription would be with all getters and setters

public class EmployeeJobDescription {

private Integer id;
private String jobDescription;

public EmployeeJobDescription(Integer id) {
    this.id = id;
}

public EmployeeJobDescription(Integer id, String jobDescription) {
    this.id = id;
    this.jobDescription = jobDescription;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getJobDescription() {
    return jobDescription;
}

public void setJobDescription(String jobDescription) {
    this.jobDescription = jobDescription;
}

}

Found out how to do that in this post:

Convert a String to an ObservableValue

Problem

I have the next class ``` public class ProductStockDto extends private Long id; private Long amount; private ProductDto product; private StockDto stock; //getters and setters... } ``` In JavaFx I have my table and I want to bind the product.name property to the column, something like this. ``` ObservableList<ProductStockDto> data = FXCollections.observableArrayList(); data.addAll(products); nameColumn.setCellValueFactory(new PropertyValueFactory("product.name")); productTable.setItems(data); ``` But when I do that, the rows on the TableView appears on blank. Somebody can help me with this? I want to bind nested object properties, on Java Swing was something like that ${product.name} Thanks.

Original source