Bind nested object properties to tableview in JavaFX
•
Java
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 add product The name attribute is bound to the column, like this
ObservableList<ProductStockDto> data = FXCollections.observableArrayList();
data.addAll(products);
nameColumn.setCellValueFactory(new PropertyValueFactory("product.name"));
productTable.setItems(data);
But when I do, the rows on the tableview appear blank
Can someone help me with this? I want to bind nested object properties, which is like ${product. Name} on Java swing
thank you.
Solution
JavaFX does not support this format. As a solution, you can try this method:
nameColumn.setCellValueFactory(new Callback<CellDataFeatures<ProductStockDto,String>,ObservableValue<String>>() {
@Override
public ObservableValue<String> call(CellDataFeatures<ProductStockDto,String> data){
return data.getValue().getProducts().nameproperty();
}
});
Where productdto will own
public class ProductDto{
private StringProperty name = new SimpleStringProperty("Itachi");
public String getName() {
return name.get();
}
public void setStreet(String name) {
this.name.set(name);
}
public StringProperty nameproperty(){
return name;
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
