条件绑定无法按预期工作
看看Layout
-> initPanelSizeBinding()
。问题then()
正在执行,即使panelProperty.get()
是null
。难道我做错了什么?
您可以自己尝试一下,下面是完整的可重现示例。(在 OpenJFX 15 上测试)
可重现的示例 -单击此处(pastebin)
或者看看下面:
代码:
主程序
public class Main {
public static void main(String[] args) {
Layout layout = new Layout();
/*
WARNING: Exception while evaluating binding
java.lang.NullPointerException: Cannot invoke "Panel.getWidth()" because the return value of "javafx.beans.property.ObjectProperty.get()" is null
at Layout.lambda$initPanelSizeBinding$0(Layout.java:20)
at javafx.beans.binding.Bindings$6.computeValue(Bindings.java:358)
at javafx.beans.binding.ObjectBinding.get(ObjectBinding.java:157)
at javafx.beans.binding.ObjectExpression.getValue(ObjectExpression.java:49)
at com.sun.javafx.binding.ExpressionHelper.addListener(ExpressionHelper.java:53)
at javafx.beans.binding.ObjectBinding.addListener(ObjectBinding.java:77)
at javafx.beans.binding.When$ObjectCondition.<init>(When.java:757)
at javafx.beans.binding.When$ObjectConditionBuilder.otherwise(When.java:854)
at Layout.initPanelSizeBinding(Layout.java:24)
at Layout.<init>(Layout.java:12)
at Main.main(Main.java:4)
*/
}
}
布局.java
public final class Layout {
public Layout() {
panelSize.bind(initPanelSizeBinding(panel));
// there's no need to do anything with panel, because it's an example
}
private ObjectBinding<Dimension2D> initPanelSizeBinding(ObjectProperty<Panel> panelProperty) {
return Bindings
.when(panelProperty.isNotNull())
.then(Bindings.createObjectBinding(
() -> new Dimension2D(panelProperty.get().getWidth(), panelProperty.get().getHeight()),
Bindings.selectDouble(panelProperty, "width"),
Bindings.selectDouble(panelProperty, "height"))
)
.otherwise(new Dimension2D(150.0, 150.0));
}
//------Properties
//panel
private final ObjectProperty<Panel> panel = new SimpleObjectProperty<>
(Layout.this, "panel", null);
public ObjectProperty<Panel> panelProperty() {
return panel;
}
public void setPanel(Panel value) {
panel.set(value);
}
public Panel getPanel() {
return panel.get();
}
//panelSize
private final ReadOnlyObjectWrapper<Dimension2D> panelSize = new ReadOnlyObjectWrapper<>
(Layout.this, "panelSize");
public ReadOnlyObjectProperty<Dimension2D> panelSizeProperty() {
return panelSize.getReadOnlyProperty();
}
public Dimension2D getPanelSize() {
return panelSize.get();
}
}
面板.java
public final class Panel {
private final VBox rootNode;
public Panel() {
rootNode = new VBox();
width.bind(rootNode.widthProperty());
height.bind(rootNode.heightProperty());
}
public VBox getRootNode() {
return rootNode;
}
//------Properties
//width
private final ReadOnlyDoubleWrapper width = new ReadOnlyDoubleWrapper
(Panel.this, "width");
public ReadOnlyDoubleProperty widthProperty() {
return width.getReadOnlyProperty();
}
public double getWidth() {
return width.get();
}
//height
private final ReadOnlyDoubleWrapper height = new ReadOnlyDoubleWrapper
(Panel.this, "height");
public ReadOnlyDoubleProperty heightProperty() {
return height.getReadOnlyProperty();
}
public double getHeight() {
return height.get();
}
}
回答
的then
和otherwise
绑定,至少作为JavaFX的15,评价热切1。我不确定他们是否应该急切地评估1,但确实如此。因此,尽管您可能会根据panelProperty.isNotNull()
您创建的绑定进行调节,但createObjectBinding
无论如何仍会尝试计算该值。在这种情况下,最好放弃使用Bindings.when
并在自定义绑定中自己实现逻辑:
private ObjectBinding<Dimension2D> initPanelSizeBinding(ObjectProperty<Panel> panelProperty) {
DoubleBinding width = Bindings.selectDouble(panelProperty, "width");
DoubleBinding height = Bindings.selectDouble(panelProperty, "height");
return Bindings.createObjectBinding(
() -> {
Panel panel = panelProperty.get();
if (panel == null) {
return new Dimension2D(150.0, 150.0);
}
return new Dimension2D(width.get(), height.get());
},
panelProperty,
width,
height);
}
1.仔细观察,条件绑定似乎确实试图偷懒。不幸的是,条件绑定向then
observable添加侦听器的行为导致所述 observable 被验证(并因此急切地评估)。这是实现方式的结果ExpressionHelper
,需要对其observable.getValue()
进行验证。我不明白为什么代码会这样做,特别是考虑到它是一个InvalidationListener
被添加的东西,它应该是懒惰的监听器。