java - Bind property to ObjectProperty's property -
i trying make line points node in property:
objectproperty<node> pointedtonode = new simpleobjectproperty();
i want line point so:
line.endxproperty().bind(pointedtonode.get().layoutxproperty());
the problem when pointedtonode
changes, binded value (pointedtonode.get()
) has changed, , layoutxproperty()
no longer valid.
i need line invisible if value of pointedtonode
null.
i tried custom double bindings no success.
using plain api:
changelistener<number> xlistener = (obs, oldx, newx) -> line.setendx(newx.doublevalue()); pointedtonode.addlistener((obs, oldnode, newnode) -> { if (oldnode != null) { oldnode.layoutxproperty().removelistener(xlistener); } if (newnode != null) { newnode.layoutxproperty().addlistener(listener); } });
or, using easybind framework:
line.endxproperty().bind( easybind.select(pointedtonode) .selectobject(node::layoutxproperty) .orelse(-1)); // value if pointedtonode null
you can try using bindings.select
(standard) api, looks bit easybind has several disadvantages: doesn't deal elegantly null
values in "chain" (i.e. if pointedtonode.get()
null
); not typesafe; , prone premature garbage collection.
line.endxproperty().bind(bindings.selectdouble(pointedtonode, "layoutx"));
i recommend easybind approach, or, if don't want use third-party library reason, use "by hand" listener approach in first code block.
for visibility, do
line.visibleproperty().bind(pointedtonode.isnull());
Comments
Post a Comment