如何更新对象的 JavaFX 绑定引用

How to update JavaFX binding reference of an Object

考虑以下型号,Apple

public class Apple {
    private StringProperty appleName = new SimpleStringProperty("Apple");

    public String getAppleName() {
        return appleName.get();
    }

    public StringProperty appleNameProperty() {
        return appleName;
    }

    public void setAppleName(String appleName) {
        this.appleName.set(appleName);
    }
}

和篮子

public class Basket {
    private Apple apple = new Apple();

    public Apple getApple() {
        return apple;
    }

    public void setApple(Apple apple) {
        this.apple = apple;
    }
}

篮子一个苹果。现在,我正在尝试绑定一个简单的字符串 属性,如下所示。

public class Food{

    public static void main(String[] args) {
        StringProperty localApple = new SimpleStringProperty("lGreenApple");
        Basket basket = new Basket();
        Apple rGreenApple = new Apple();
        rGreenApple.setAppleName("rGreenApple");
        basket.setApple(rGreenApple);
        Bindings.bindBidirectional(localApple, rGreenApple.appleNameProperty());
        rGreenApple.appleNameProperty().set("rGreenApple 2");
        System.out.println(localApple.getValue()); //rGreenApple 2

        Apple redApple = new Apple();
        redApple.setAppleName("rRedApple");
        basket.setApple(redApple);
        redApple.appleNameProperty().set("rRedApple 2");
        System.out.println(localApple.getValue());//Still rGreenApple 2
    }
}

绑定后尝试检索值时,localApple 对象仍然引用了 rGreenApple。获得红苹果的干净方法是什么?

你需要两件事:

  1. 使 Basket 中的 apple 属性 成为 JavaFX 可观察对象 属性, 而不是常规的 JavaBean 样式 属性。
  2. 将本地字符串 属性 绑定到 "property of a property",即 appleName 属性 apple 属性 Basket.

第一部分很简单:

public class Basket {

    private final ObjectProperty<Apple> apple = new SimpleObjectProperty<>();

    public ObjectProperty<Apple> appleProperty() {
        return apple ;
    }

    public final Apple getApple() {
        return appleProperty().get();
    }

    public final void setApple(Apple apple) {
        appleProperty().set(apple);
    }
}

对于第二部分,最干净的方法是使用 EasyBind framework:

public class Food{

    public static void main(String[] args) {
        Basket basket = new Basket();
        Apple rGreenApple = new Apple();
        rGreenApple.setAppleName("rGreenApple");
        basket.setApple(rGreenApple);

        Property<String> localApple = EasyBind.monadic(basket.appleProperty())
            .selectProperty(Apple::appleNameProperty);

        rGreenApple.appleNameProperty().set("rGreenApple 2");
        System.out.println(localApple.getValue()); //rGreenApple 2

        Apple redApple = new Apple();
        redApple.setAppleName("rRedApple");
        basket.setApple(redApple);

        System.out.println(localApple.getValue());// rRedApple

        redApple.appleNameProperty().set("rRedApple 2");
        System.out.println(localApple.getValue());// rRedApple 2
    }
}

如果没有 EasyBind,您必须手动管理中间侦听器。类似于:

    StringProperty localApple = new SimpleStringProperty();
    localApple.bindBidirectional(basket.getApple().appleNameProperty());
    basket.appleProperty().addListener((obs, oldApple, newApple) -> {
        if (oldApple != null) {
            localApple.unbindBidirectional(oldApple.appleNameProperty());
        }
        if (newApple == null) {
            localApple.set("");
        } else {
            localApple.bindBidirectional(newApple.appleNameProperty());
        }
    });