Binding nested object properties to TableView in JavaFx


Question

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.

1
9
7/15/2014 10:32:14 PM

This format is not supported in Javafx, as a work around, you can try something like this :

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 have

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;
    }
}
5
7/16/2014 7:05:13 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon