在JavaFX中有一些属性,比如boundsInParentProperty(),它是Bounds类型的。这个类有几个组件,它们是不可观察的。
那么,如果我想绑定其中一个呢?
例如,到Bounds#getMinX()
我们有一些bind...函数来提取属性的组件吗?我是否可以确保侦听器在不同的组件更改时会得到通知?
发布于 2016-05-19 16:20:19
Bindings.selectDouble可用于创建此绑定:
DoubleBinding minXBinding = Bindings.selectDouble(node.boundsInParentProperty(), "minX");我是否可以确保侦听器在不同的组件更改时会得到通知?
是。尽管没有文档表明Bounds是不可变的,但是通过查看源代码,您可以发现:
minX )存储在具有private访问权限的字段中。final。因此,Bounds实际上是不可变的,因此,如果边界发生变化,则可以确保必须替换属性的值。
发布于 2016-05-19 15:36:15
对于boundsInParentProperty(),您可以使用Bindings.createDoubleBinding。
DoubleBinding minXBinding = Bindings.createDoubleBinding(() -> boundsInParentProperty.get().getMinX(), boundsInParentProperty);
DoubleProperty minX = new SimpleDoubleProperty();
minX.bind(minXBinding);https://stackoverflow.com/questions/37322140
复制相似问题