Javafx PropertyValueFactory not populating Tableview -
this has baffled me while , cannot seem grasp of it. i'm using cell value factory populate simple 1 column table , not populate in table.
it , click rows populated not see values in them- in case string values. [i edited make clearer]
i have different project under works under same kind of data model. doing wrong?
here's code. commented code @ end seems work though. i've checked see if usual mistakes- creating new column instance or new tableview instance, there. nothing. please help!
//simple data model stock.java
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); } }
//controller class mainguicontroller.java
private observablelist<stock> data; @fxml private tableview<stock> stocktableview;// = new tableview<>(data); @fxml private tablecolumn<stock, string> tickercol; private void settickerstocol() { try { statement stmt = conn.createstatement();//conn defined , works resultset rsltset = stmt.executequery("select ticker tickerlist order ticker"); data = fxcollections.observablearraylist(); stock stockinstance; while (rsltset.next()) { stockinstance = new stock(rsltset.getstring(1).touppercase()); data.add(stockinstance); } } catch (sqlexception ex) { logger.getlogger(writetofile.class.getname()).log(level.severe, null, ex); system.out.println("connection failed! check output console"); } tickercol.setcellvaluefactory(new propertyvaluefactory<stock,string>("stockticker")); stocktableview.setitems(data); } /*this, on other hand, works*/ /*callback<celldatafeatures<stock, string>, observablevalue<string>> celldatafeat = new callback<celldatafeatures<stock, string>, observablevalue<string>>() { @override public observablevalue<string> call(celldatafeatures<stock, string> p) { return new simplestringproperty(p.getvalue().getstockticker()); } };*/
how fix it
the case of getter , setter methods wrong.
getstockticker
should getstockticker
setstockticker
should setstockticker
some background information
your propertyvaluefactory remains same with:
new propertyvaluefactory<stock,string>("stockticker")
the naming convention seem more obvious when add property accessor 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 find relevant accessors. first try use stocktickerproperty accessor and, if not present fall getters , setters. providing property accessor recommended automatically enable table observe property in underlying model, dynamically updating it's data underlying model changes.
Comments
Post a Comment