java - How to update TreeTableView data? -
i writing app measures time spent on tasks. have tabletreeview contains details of project , tasks.
this how looks:tabletreeviewimage
what want make duration`s column updating values when task running. project duration should updated.
this model:
public abstract class simpletask{ abstract duration getduration(); } public class task extends simpletask{ duration duration; @override public duration getduration(){ return duration; } public start(){ //update duration value every second; } } public class project extends simpletask{ list<task> tasks; @override public duration getduration(){ return tasks.stream() .map(task::getduration) .reduce(duration.zero,duration::plus); }
i have tried bind duration column method getduration() tabletreeview needs refreshed update values in column.
durat.setcellvaluefactory(new callback<treetablecolumn.celldatafeatures<simpletask, duration>, observablevalue<duration>>() { @override public observablevalue<duration> call(treetablecolumn.celldatafeatures<generaltask, duration> simpletaskdurationcelldatafeatures) { return new simpleobjectproperty<duration>(simpletaskdurationcelldatafeatures.getvalue().getvalue().getduration()); } });
how can make duration column updating values when task running?
simply add readonlyobjectproperty<duration>
simpletask
, use instead of new simpleobjectproperty
created cellvaluefactory
. otherwise you'd need "manually trigger" update table using tableview.refresh
(available starting javafx 8u60) every time duration modified:
public abstract class simpletask { public abstract duration getduration(); public abstract readonlyobjectproperty<duration> durationproperty(); } public class task extends simpletask { private final readonlyobjectwrapper<duration> duration = new readonlyobjectwrapper<>(); @override public duration getduration() { return duration.get(); } public readonlyobjectproperty<duration> durationproperty() { return duration.getreadonlyproperty(); } public void start() { //update duration value every second using this.duration.set(newduration); } } public class project extends simpletask { private final observablelist<task> tasks; private final readonlyobjectwrapper<duration> duration; public project() { this.duration = new readonlyobjectwrapper<>(); this.tasks = fxcollections.observablearraylist(t -> new observable[]{t.durationproperty()}); this.duration.bind(bindings.createobjectbinding(() -> tasks.stream() .map(task::getduration) .reduce(duration.zero, duration::plus), this.tasks)); } @override public duration getduration() { return duration.get(); } public readonlyobjectproperty<duration> durationproperty() { return duration.getreadonlyproperty(); } }
durat.setcellvaluefactory(new callback<treetablecolumn.celldatafeatures<simpletask, duration>, observablevalue<duration>>() { @override public observablevalue<duration> call(treetablecolumn.celldatafeatures<simpletask, duration> simpletaskdurationcelldatafeatures) { return simpletaskdurationcelldatafeatures.getvalue().getvalue().durationproperty(); } });
note reduce code duplication extracting members except start()
task
abstract class, adding protected
methods access property itself, not it's readonly version , extending class project
too.
Comments
Post a Comment