listview - Update List-View when model change -
i'm not able update javafx.scene.control.listview<todo>
control. can see listview displays headline property. when change headline of todo list-view still displays old value.
public class todo { private stringproperty headline = new simplestringproperty(); private stringproperty description = new simplestringproperty(); public todo(string aheadline) { this.setheadline(aheadline); } public todo(string aheadline, string adescription) { this(aheadline); this.description.set(adescription); } public string getdescription() { return this.description.get(); } public stringproperty descriptionproperty() { return this.description; } public void setdescription(string adescription) { this.description.set(adescription); } public string getheadline() { return this.headline.get(); } public void setheadline(string aheadline) { this.headline.set(aheadline); } public static observablelist<todo> getmockups() { try { return fxcollections.observablearraylist(todoxmlparser.gettodosfromxml(paths.get("/todos.xml"))); } catch (jdomexception | ioexception e) { e.printstacktrace(); } return null; } @override public string tostring() { return this.headline.get(); }
}
this.listview = new listview<>(); this.listview.setitems(todo.getmockups()); this.listview.getselectionmodel().getselecteditem().setheadline("new");
i'm missing method listview.refresh(), or update()
what's right way this?
edit: find way of doing this, glad, if can explain "official" way of doing this.
void updatelistview(todo todo) { int index = this.listview.getitems().indexof(todo); this.listview.getitems().remove(todo); this.listview.getitems().add(index, todo); }
i found workaround. not beautifull job. i'll still dig better way it...
int index = listview.getselectionmodel().getselectedindex(); system.out.println(listview.getitems()); observablelist<todo> olist = listview.getitems(); olist.get(index).setheadline("new"); listview.setitems(null); listview.setitems(olist);
or
listview.getselectionmodel().getselecteditem().setheadline("new 2"); observablelist<todo> olist = listview.getitems(); listview.setitems(null); listview.setitems(olist);
here's "better" way
make little change in todo class have getheadline()
method returns stringproperty instead of string.
then, make own implementation of cellfactory , bind textproperty stringproperty given getheadline() method :
listview.setcellfactory(new callback<listview<todo>, listcell<todo>>() { public listcell<todo> call(listview<todo> param) { final listcell<todo> cell = new listcell<todo>() { @override public void updateitem(todo item, boolean empty) { super.updateitem(item, empty); if (item != null) { textproperty().bind(item.getheadline()); } } }; return cell; } });
now when make change underlying list corresponding cell updated
Comments
Post a Comment