跟踪JavaFX中绑定的节点的更改 [英] track changes of nodes bound in JavaFX

查看:134
本文介绍了跟踪JavaFX中绑定的节点的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要跟踪javafx中节点的绝对位置和大小的变化。由于调整窗口或用户操作的大小而导致的更改,...

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!

请帮助我,谢谢。

推荐答案

这在性能上相当沉重,所以我不建议这样做场景图中有大量节点,但是:

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);

这篇关于跟踪JavaFX中绑定的节点的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆