java - Are bindings automatically removed in a TreeCell -
java - Are bindings automatically removed in a TreeCell -
i have treeview has cell mill set on it. treecells i'm returning displayed below:
import javafx.beans.binding.stringbinding; import javafx.collections.observablemap; import javafx.scene.control.treecell; public class treecelltest extends treecell<string> { private observablemap<string, stringbinding> lookup; public treecelltest(observablemap<string, stringbinding> lookup) { this.lookup = lookup; } @override protected void updateitem(string id, boolean empty) { super.updateitem(id, empty); if (empty) { settext(null); } else { stringbinding stringbinding = lookup.get(id); textproperty().bind(stringbinding); } } }
notice i'm not setting text i'm binding textproperty stringbinding. works fine in normal situations i'm wondering if ok utilize within treecell.
the treecell gets recycled , when needed know whether when happens binding gets automatically removed or whether need remove manually?
i don't want case each treecell has 100's of bindings attached it.
while it's not documented, appears calling bind(...)
remove existing bindings before creating new binding.
for example:
import javafx.beans.property.simplestringproperty; import javafx.beans.property.stringproperty; public class rebindingtest { public static void main(string[] args) { stringproperty text = new simplestringproperty(); stringproperty value1 = new simplestringproperty(); stringproperty value2 = new simplestringproperty(); text.addlistener((obs, oldvalue, newvalue) -> system.out.printf("text changed %s %s%n", oldvalue, newvalue)); text.bind(value1); value1.set("set value 1"); text.bind(value2); value2.set("set value 2"); value1.set("reset value 1"); } }
so think need create code work correctly add
textproperty().unbind();
to if (empty) { ... }
block.
of course, calling unconditionally in updateitem(...)
method mean you're not relying on undocumented behavior, , loss of efficiency minimal.
java javafx-8
Comments
Post a Comment