track changes of nodes bound in JavaFX

我只是一个虾纸丫 提交于 2019-12-12 17:58:19

问题


I need to track changes in absolute position and size of a node in javafx.Any change that is caused by resizing window or user manipulation,...

 node.boundsInLocalProperty().addListener(new ChangeListener<Bounds>() {

 @Override
 public void changed(ObservableValue<? extends Bounds> observable,Bounds oldValue, Bounds newValue) {

 System.err.println("Changed!");


 }

 });


 node.boundsInParentProperty().addListener(new ChangeListener<Bounds>() {

 @Override
 public void changed(ObservableValue<? extends Bounds> observable,Bounds oldValue, Bounds newValue) {

 System.err.println("Changed!");


 }

 });

I tried these solutions but doesn't work!

please help me, thanks.


回答1:


This is quite heavy on performance, so I do not recommend doing this with a large number of nodes in the scene graph, but:

ObjectBinding<Bounds> boundsInScene = Bindings.createObjectBinding(
    () -> node.localToScene(node.getBoundsInLocal()),
    node.localToSceneTransformProperty(),
    node.boundsInLocalProperty());

boundsInScene.addListener((obs, oldBounds, newBounds) -> 
    System.err.println("Changed!"));

You can also do the following, which might be less prone to premature garbage collection:

ChangeListener<Object> listener = (obs, oldValue, newValue) -> 
    System.err.println("New bounds in scene: "+node.localToScene(node.getBoundsInLocal()));
node.localToSceneTransformProperty().addListener(listener);
node.boundsInLocalProperty().addListener(listener);


来源:https://stackoverflow.com/questions/31389834/track-changes-of-nodes-bound-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!