Javafx PropertyValueFactory not populating Tableview

后端 未结 2 1198
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 05:03

This has baffled me for a while now and I cannot seem to get the grasp of it. I\'m using Cell Value Factory to populate a simple one column table and it does not populate in

相关标签:
2条回答
  • 2020-11-22 05:37

    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. 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 it's data as the underlying model changes.

    0 讨论(0)
  • 2020-11-22 05:46

    put the Getter and Setter method in you data class for all the elements.

    0 讨论(0)
提交回复
热议问题