java - track change of nodes bound in javafx -
i want same thing asked in post track changes of nodes bound in javafx
i have : (edited) sscce:
public class floatcircle { node node; circle rectangle; bounds localtoscreen; static arraylist<objectbinding> list = new arraylist<>(); private objectbinding<bounds> boundsinscene ; pane root ; public floatcircle(node node,pane root) { this.node = node; this.root =root; this.rectangle = new circle(); this.rectangle.setmanaged(false); initsetting(); } public circle getfloatcircle() { return rectangle; } public void initsetting() { boundsinscene = bindings.createobjectbinding( () -> node.localtoscene(node.getboundsinlocal()), node.localtoscenetransformproperty(), node.boundsinlocalproperty()); boundsinscene.addlistener(new changelistener<bounds>() { @override public void changed(observablevalue<? extends bounds> observable, bounds oldvalue, bounds newvalue) { setlocation(newvalue); system.err.println("changed!!!!!!"); } }); localtoscreen = node.localtoscene(node.getboundsinlocal()); setlocation(localtoscreen); addcircle(); } public void setlocation(bounds localtoscreen) { int r = 10; rectangle.setcenterx(localtoscreen.getminx() - r); rectangle.setcentery(localtoscreen.getminy() - 5); rectangle.setradius(r); // return rect; } public void addcircle(){ root.getchildren().add(rectangle); } } public class selfcontained extends application { arraylist<node> nodes = new arraylist<>(); @override public void start(stage primarystage) { button btn = new button(); stackpane root = new stackpane(); root.getchildren().add(btn); nodes.add(btn); scene scene = new scene(root, 300, 250); primarystage.settitle("hello world!"); primarystage.setscene(scene); primarystage.show(); (int = 0; < nodes.size(); i++) { node n = nodes.get(i); floatcircle floatcircle = new floatcircle(n, root); } } /** * @param args command line arguments */ public static void main(string[] args) { launch(args); } }
but problem changed method doesn't called correctly...it called 1 time....if add code in changed method instead of old 1 called correctly...but very slow
floatcircle fc = new floatcircle(node); fc.rectangle = rectangle ; fc.setlocation(newvalue, directionid, alignment);
can me? thank's...
since button
, circle
in example in same parent pane, more (and efficiently) binding circle's location button's boundsinparentproperty()
. assume doing way because have real application in 2 nodes in different parents.
it looks localtoscenetransformproperty()
getting garbage collected prematurely (see this). can fix using listener instead of binding:
public static class floatcircle { node node; circle rectangle; pane root; public floatcircle(node node, pane root) { this.node = node; this.root = root; this.rectangle = new circle(); this.rectangle.setmanaged(false); initsetting(); } public circle getfloatcircle() { return rectangle; } public void initsetting() { changelistener<object> updater = (obs, oldvalue, newvalue) -> setlocation(node.localtoscene(node.getboundsinlocal())); node.boundsinlocalproperty().addlistener(updater); node.localtoscenetransformproperty().addlistener(updater); setlocation(node.localtoscene(node.getboundsinlocal())); addcircle(); } public void setlocation(bounds localtoscreen) { int r = 10; rectangle.setcenterx(localtoscreen.getminx() - r); rectangle.setcentery(localtoscreen.getminy() - 5); rectangle.setradius(r); } public void addcircle() { root.getchildren().add(rectangle); } }
Comments
Post a Comment