Javafx PropertyValueFactory not populating Tableview

Suggested solution (use a Lambda, not a PropertyValueFactory)

Instead of:

aColumn.setCellValueFactory(new PropertyValueFactory<Appointment,LocalDate>("date"));

Write:

aColumn.setCellValueFactory(cellData -> cellData.getValue().dateProperty());

For more information, see this answer:


Solution using PropertyValueFactory

The lambda solution outlined above is preferred, but if you wish to use PropertyValueFactory, this alternate solution provides information on that.

How to Fix It

The case of your getter and setter methods are wrong.

getstockTicker should be getStockTicker

setstockTicker should be setStockTicker

Some Background Information

Your PropertyValueFactory remains the same with:

new PropertyValueFactory<Stock,String>("stockTicker")

The naming convention will seem more obvious when you also add a property accessor to your Stock class:

public class Stock {

    private SimpleStringProperty stockTicker;

    public Stock(String stockTicker) {
        this.stockTicker = new SimpleStringProperty(stockTicker);
    }

    public String getStockTicker() {
        return stockTicker.get();
    }

    public void setStockTicker(String stockticker) {
        stockTicker.set(stockticker);
    }

    public StringProperty stockTickerProperty() {
        return stockTicker;
    }
}

The PropertyValueFactory uses reflection to find the relevant accessors (these should be public). First, it will try to use the stockTickerProperty accessor and, if that is not present fall back to getters and setters. Providing a property accessor is recommended as then you will automatically enable your table to observe the property in the underlying model, dynamically updating its data as the underlying model changes.

Leave a Comment