java - Items not showing in TableView in JavaFX -
i have table view pintable in fxmland 2 table columns i.e. latcolumn, longcolumn. have followed following method populate table :
https://docs.oracle.com/javafx/2/ui_controls/table-view.htm
snippets follows:
fxml controller class:
@fxml public tableview pintable; @fxml public tablecolumn latcolumn, longcolumn; public final observablelist<pinlist> datasource = new fxmlcollections.observablearraylist(); @override public void initialize(url url, resourcebundle rb) { inittable(); } private void inittable() { latcolumn.setcellvaluefactory(new propertyvaluefactory<pinlist,string>("latpin")); longcolumn.setcellvaluefactory(new propertyvaluefactory<pinlist,string>("longpin")); pintable.setitems(datasource); } @fxml private void addbuttonclicked(mouseevent event) { if(lattext.gettext().equals("")) { system.out.println("lat empty"); } else if(longtext.gettext().equals("")) { system.out.println("long empty"); } else { latval = double.parsedouble(lattext.gettext()); longval = double.parsedouble(longtext.gettext()); datasource.add(new pinlist(lattext.gettext(),longtext.gettext(),descriptiontext.gettext())); pintable.getitems().clear(); pintable.getitems().addall(datasource); for(pinlist p: datasource) { system.out.print(p.getlatpin() + " " + p.getlongpin() + " " + p.getdescriptionpin() + "\n"); } } } i have pinlist class follows:
public class pinlist { private final simplestringproperty latpin; private final simplestringproperty longpin; private string descriptionpin; public pinlist(string _latpin, string _longpin, string _descriptionpin) { this.latpin = new simplestringproperty(_latpin); this.longpin = new simplestringproperty(_longpin); this.descriptionpin = _descriptionpin; } public string getlatpin() { return latpin.getvalue(); } public stringproperty latpinproperty() { return latpin; } public string getlongpin() { return longpin.getvalue(); } public stringproperty longpinproperty() { return longpin; } public string getdescriptionpin() { return descriptionpin; } } all these seem fine. but, when click add button, nothing happens. no row created in table , println inside addbuttonclicked event handler doesn't execute, or executed no data in datasource whatsoever. appreciated.
in inittable() method do
pintable.setitems(datasource); so list held internally pintable in items property same list datasource (they identical reference).
now in event handler method do
datasource.add(new pinlist(...)); which adds new item datasource (which same list table's items)
and then
pintable.getitems().clear(); which removes elements table's items list. table's items list empty (has no elements). of course, since same list datasource, have removed items datasource: same (now empty) list pintable.getitems().
and do
pintable.getitems().addall(datasource); which copies items in datasource (there none) pintable.getitems() (which same list). duplicates items in list, since emptied list, still have empty list.
just remove lines
pintable.getitems().clear(); pintable.getitems().addall(datasource); all want here is:
@fxml private void addbuttonclicked(mouseevent event) { if(lattext.gettext().equals("")) { system.out.println("lat empty"); } else if(longtext.gettext().equals("")) { system.out.println("long empty"); } else { latval = double.parsedouble(lattext.gettext()); longval = double.parsedouble(longtext.gettext()); datasource.add(new pinlist(lattext.gettext(),longtext.gettext(),descriptiontext.gettext())); for(pinlist p: datasource) { system.out.print(p.getlatpin() + " " + p.getlongpin() + " " + p.getdescriptionpin() + "\n"); } } }
Comments
Post a Comment