java - TableView doesn't update all columns -
i'm trying javafx making simple applications. right i'm working on can called "employees database" mean store data in table , tree, tree sorts employees department. when hit "add worker" label in menubar hbox displaying, , after filing textfields , clicking "add" button written data should appear in table. problem tableview updates of columns except "years worked" , "department" column. read tips cannonical variable names proper valuefactory usage stil can't on this.
public class main extends application { wtable table = new wtable(); wtree tree = new wtree(); wmenu menu = new wmenu(); wtextfield addfirstname = new wtextfield("first name"); wtextfield addsecondname = new wtextfield("second name"); wtextfield addlastname = new wtextfield("last name"); wtextfield adddep = new wtextfield("department"); wtextfield addyears = new wtextfield("years worked"); wtextfield addsalary = new wtextfield("salary"); wtextfield addage = new wtextfield("age"); wbutton addbutton = new wbutton("add"); observablelist<model> mlist = fxcollections.observablearraylist(); hbox addbox = new hbox(); private class wtable extends tableview<model> { private tablecolumn<model, string> fnamecol; private tablecolumn<model, string> lnamecol; private tablecolumn<model, string> snamecol; private tablecolumn<model, string> depcol; private tablecolumn<model, string> yearcol; private tablecolumn<model, string> salarycol; private tablecolumn<model, string> agecol; public wtable() { super(); fnamecol = new tablecolumn<model, string>("first name"); fnamecol.setminwidth(100); fnamecol.setcellvaluefactory( new propertyvaluefactory<model, string>("firstname")); snamecol = new tablecolumn<model, string>("second name"); snamecol.setminwidth(100); snamecol.setcellvaluefactory( new propertyvaluefactory<model, string>("secondname")); lnamecol = new tablecolumn<model, string>("last name"); lnamecol.setminwidth(100); lnamecol.setcellvaluefactory( new propertyvaluefactory<model, string>("lastname")); depcol = new tablecolumn<model, string>("department"); depcol.setminwidth(100); depcol.setcellvaluefactory( new propertyvaluefactory<model, string>("department")); yearcol = new tablecolumn<model, string>("years worked"); yearcol.setminwidth(100); yearcol.setcellvaluefactory( new propertyvaluefactory<model, string>("yearsworked")); salarycol = new tablecolumn<model, string>("salary"); salarycol.setminwidth(100); salarycol.setcellvaluefactory( new propertyvaluefactory<model, string>("salary")); agecol = new tablecolumn<model, string>("age"); agecol.setminwidth(100); agecol.setcellvaluefactory( new propertyvaluefactory<model, string>("age")); this.getcolumns().addall(fnamecol, snamecol, lnamecol, depcol, yearcol, salarycol, agecol); this.seteditable(false); } } private class wbutton extends button { public wbutton(string desc) { super(desc); this.setprefwidth(50); this.setonmouseclicked(new eventhandler<mouseevent>() { @override public void handle(mouseevent event) { mlist.add(new model(addfirstname.gettext(), addsecondname.gettext(), addlastname.gettext(), adddep.gettext(), addage.gettext(), addyears.gettext(), addsalary.gettext())); } }); } } private class wtree extends treeview<string> { private treeitem<string> rootitem; private treeitem<string> sitem; private treeitem<string> aitem; private treeitem<string> iitem; private treeitem<string> uitem; public wtree() { super(); rootitem = new treeitem<string>("employees"); sitem = new treeitem<string>("sales department"); aitem = new treeitem<string>("accounts department"); iitem = new treeitem<string>("it support"); uitem = new treeitem<string>("undercover"); this.setroot(rootitem); rootitem.getchildren().addall(sitem, aitem, iitem, uitem); rootitem.setexpanded(true); } } private class wtextfield extends textfield { public wtextfield(string desc) { super(); this.setprompttext(desc); this.setprefwidth(100); } } private final class wmenu extends menubar { public wmenu() { super(); menu menuadd = new menu("add"); menu menuedit = new menu("edit"); menu addworker = new menu("add worker"); menu adddepartment = new menu("add department"); menu editworker = new menu("edit worker"); addworker.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent t) { addbox.setvisible(true); } }); this.getmenus().addall(menuadd, menuedit); menuadd.getitems().addall(addworker, adddepartment); menuedit.getitems().addall(editworker); } } @override public void start(stage primarystage) { try { primarystage.settitle("employees"); table.setitems(mlist); gridpane grid = new gridpane(); addbox.getchildren().addall(addfirstname, addsecondname, addlastname, adddep, addyears, addsalary, addage, addbutton); addbox.setvisible(false); grid.add(menu, 0, 0, 2, 1); grid.add(table, 1, 1); grid.add(tree, 0, 1); grid.add(addbox, 0, 2, 2, 1); scene scene = new scene(grid); primarystage.setscene(scene); primarystage.show(); } catch (exception e) { e.printstacktrace(); } } public static void main(string[] args) { launch(args); } }
and model class
public class model { private final simplestringproperty firstnameproperty; private final simplestringproperty secondnameproperty; private final simplestringproperty lastnameproperty; private final simplestringproperty departmentproperty; private final simplestringproperty ageproperty; private final simplestringproperty yearsworkedproperty; private final simplestringproperty salaryproperty; public model(string fname, string sname, string lname, string func, string age, string years, string salary) { this.firstnameproperty = new simplestringproperty(fname); this.lastnameproperty = new simplestringproperty(lname); this.secondnameproperty = new simplestringproperty(sname); this.departmentproperty = new simplestringproperty(func); this.ageproperty = new simplestringproperty(age); this.yearsworkedproperty = new simplestringproperty(years); this.salaryproperty = new simplestringproperty(salary); } public void setfirstname(string newname) { firstnameproperty.set(newname); } public string getfirstname() { return firstnameproperty.get(); } public void setlastname(string newname) { lastnameproperty.set(newname); } public string getlastname() { return lastnameproperty.get(); } public void setsecondname(string newname) { secondnameproperty.set(newname); } public string getsecondname() { return secondnameproperty.get(); } public void setage(string newage) { ageproperty.set(newage); } public string getage() { return ageproperty.get(); } public void setyears(string newy) { yearsworkedproperty.set(newy); } public string getyears() { return yearsworkedproperty.get(); } public void setsalaray(string newsal) { salaryproperty.set(newsal); } public string getsalary() { return salaryproperty.get(); } public void setdepartment(string newfunc) { departmentproperty.set(newfunc); } string getdepartment() { return departmentproperty.get(); } }
the correct pattern property of type t
called x
is:
private objectproperty<t> x = new simpleobjectproperty<>(); public objectproperty<t> xproperty() { return x ; } public final t getx() { return x.get(); } public final void setx(t x) { this.x.set(x); }
with special cases primitive types , string
s: instead of objectproperty<string>
, use stringproperty
, etc.
obviously method names important; variables (field or parameters) can called like.
so model
class fails follow pattern, because:
- you don't have "property accessor" functions:
public stringproperty departmentproperty() {...}
etc. - your
getdepartament()
(sic) method not public - your
getyears()
,setyears()
methods not match property name used inpropertyvaluefactory
("years" instead of "yearsworked") - you have
getsalary()
setsalaray(...)
the "property accessors" (item 1 above) optional; if omit these propertyvaluefactory
revert using methods , wrapping result in readonlyobjectwrapper
; note though means if table editable, properties not automatically updated when edit table.
Comments
Post a Comment