2016-11-19 175 views
1

我在更大规模的应用程序中遇到了此问题,其中某些自定义绑定在源属性的值更改时未更新。JavaFX自定义绑定不起作用

我设法写了一个简单的类来复制这个问题,我真的不明白为什么会发生这种情况。下面是一个简单的测试,复制问题:

import javafx.beans.binding.BooleanBinding; 
import javafx.beans.binding.ObjectBinding; 
import javafx.beans.property.BooleanProperty; 
import javafx.beans.property.ObjectProperty; 
import javafx.beans.property.SimpleBooleanProperty; 
import javafx.beans.property.SimpleObjectProperty; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 

public class TestingBindingsFx { 

    private final ObjectProperty<MyEvent> objectProperty = new SimpleObjectProperty<MyEvent>(this, "objectProperty"); 
    private final BooleanProperty booleanProperty = new SimpleBooleanProperty(this, "booleanProperty"); 

    private ObjectBinding<MyEvent> bindingObj; 
    private BooleanBinding bindingBool; 

    public TestingBindingsFx(ObjectProperty<String> selection) { 
    setupBindings(selection); 
    } 

    private void setupBindings(ObjectProperty<String> selection) { 
    bindingObj = createObjectBinding(selection); 
    bindingBool = createBooleanBinding(selection); 
    objectProperty.bind(bindingObj); 
    booleanProperty.bind(bindingBool); 
    } 

    private static ObjectBinding<MyEvent> createObjectBinding(ObjectProperty<String> selection) { 
    return new ObjectBinding<MyEvent>() { 
     { 
     super.bind(selection); 
     } 

     @Override 
     protected MyEvent computeValue() { 
     System.out.println("createObjectBinding called"); 
     MyEvent ve = selection.get() == null ? MyEvent.EVENT1 
      : MyEvent.EVENT2; 
     return ve; 
     } 
    }; 
    } 

    private static BooleanBinding createBooleanBinding(ObjectProperty<String> selection) { 
    return new BooleanBinding() { 
     { 
     super.bind(selection); 
     } 

     @Override 
     protected boolean computeValue() { 
     System.out.println("createBooleanBinding called"); 
     return selection.get() == null ? true : false; 
     } 
    }; 
    } 

    public static void main(String[] args) { 
    ObjClass objclass = new ObjClass(); 
    System.out.println("Instantiating TestingBindingsFx..."); 
    TestingBindingsFx fx = new TestingBindingsFx(objclass.selection); 

    objclass.selection.addListener(new ChangeListener<String>() { 
     @Override 
     public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { 
     System.out.println("changed " + oldValue + "->" + newValue); 
     } 
    }); 

    System.out.println("Changing selection property values..."); 
    objclass.selection.set("Test 1"); 
    objclass.selection.set("Test 2"); 
    } 

    enum MyEvent { 
    EVENT1, 
    EVENT2; 
    } 

    static class ObjClass { 
    public final ObjectProperty<String> selection = new SimpleObjectProperty<String>(this, "selection"); 
    } 
} 

那么运行这个后,我看到:

Instantiating TestingBindingsFx... 
createObjectBinding called 
createBooleanBinding called 
Changing selection property values... 
changed null->Test 1 
changed Test 1->Test 2 

当我期望看到像这样:

Instantiating TestingBindingsFx... 
createObjectBinding called 
createBooleanBinding called 
Changing selection property values... 
changed null->Test 1 
createObjectBinding called 
createBooleanBinding called 
changed Test 1->Test 2 
createObjectBinding called 
createBooleanBinding called 

ChangeListener按预期工作(仅用于验证目的),每次更改选择属性的值时都会调用它。

但是,自定义绑定永远不会在第一次后更新,看着代码,我不明白为什么。起初我认为它可能与弱引用有关,但我甚至将绑定对象转换为类级变量,但没有改变。

我觉得我可能会在这里失去一些至关重要的东西,但在查看这段代码2个小时后,我只是看不出为什么。 在我的实际应用程序中,它更加奇怪,因为其中一个自定义绑定实际上可以正常工作。

回答

2

这是因为JavaFX很懒惰,认真。

如果依赖关系失效,则将绑定视为无效,并通知潜在更改。除非您添加ChangeListener或使用get来检索该值,否则不会使用computeValue方法,因为“没有人想知道新值”。这可以提高性能。

绑定属性使用InvalidationListener完成,属性也会被懒惰地刷新。

你可以例如添加更改监听器的属性强制每次绑定无效时重新计算值:

private void setupBindings(ObjectProperty<String> selection) { 
    bindingObj = createObjectBinding(selection); 
    bindingBool = createBooleanBinding(selection); 
    objectProperty.bind(bindingObj); 
    booleanProperty.bind(bindingBool); 

    objectProperty.addListener((a,b,c)-> {}); 
    booleanProperty.addListener((a,b,c)-> {}); 
} 
+0

宾果,就是这样!我知道我错过了一些简单的东西,我非常专注于不被称为的computeValue,它完全忘记了绑定的懒惰性质。我不需要强制重新计算,懒惰是完全正常的,我只是简单地缺少在更改源属性值后重新读取测试代码中的目标属性。感谢你及时的答复。 – mfc