如何更新Object的JavaFX绑定引用

时间:2015-04-06 13:57:57

标签: binding javafx-8

考虑以下型号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 个答案:

答案 0 :(得分:1)

你需要两件事:

  1. apple中将Basket属性设为JavaFX可观察属性, 而不是常规的JavaBean样式属性。
  2. 将本地字符串属性绑定到属性"的"属性,即 appleName的{​​{1}}属性的apple属性。
  3. 第一部分很简单:

    Basket

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

    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,您必须手动管理中间监听器。类似的东西:

    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
        }
    }