JavaFX中的自定义双向绑定

时间:2014-11-21 01:54:35

标签: java data-binding javafx javabeans

我试图实现一个涉及2个字段计算的GUI。我的模型有2个属性和一个绑定。

ObjectProperty<BigDecimal> price = new SimpleObjectProperty<>();
ObjectProperty<BigDecimal> quantity= new SimpleObjectProperty<>();
ObjectBinding<BigDecimal> totalPrice = new ObjectBinding<BigDecimal>() {
    { bind(price,quantity);}
    protected BigDecimal computeValue() {
        if (price.get() == null || quantity.get() == null) return null;
        return price.get().multiply(quantity.get());
    }
};

我的GUI有3个TextField来匹配价格,金额,总价格。通常,我在我的属性和我的TextField之间进行常规绑定

priceTextField.textProperty().bindBidirectional(myModel.priceProperty(), new NumberStringConverter());

现在这有点棘手。如果用户修改价格或数量,它会更新TotalPrice(这是绑定到目前为止所做的)。但我希望能够执行以下操作:如果用户更新TotalPrice,则会根据固定价格重新计算数量。

所以问题是:我如何创建这样的流=&gt; TotalPrice在价格和数量上绑定,但数量在TotalPrice和price上绑定。当我在totalPriceTextfield中输入内容时,它应该更新quantityTextField,反之亦然。

感谢。

******编辑******** 这是一段丑陋的代码,只是为了说明我想要实现的目标(nb:我知道我可以使用Binding.multiply和其他方法,但我需要将来的项目来实现计算功能)

public class TestOnBindings {

    private DoubleProperty price = new SimpleDoubleProperty(10.0);
    private DoubleProperty quantity = new SimpleDoubleProperty(1.0);
    private DoubleProperty total = new SimpleDoubleProperty(1.0);

    private DoubleBinding totalBinding = new DoubleBinding() {
        {bind(quantity,price);}
        @Override
        protected double computeValue() {
            return quantity.get()*price.get();
        }
    };

    private DoubleBinding quantityBinding = new DoubleBinding() {
        {bind(total,price);}
        @Override
        protected double computeValue() {
            return total.get()/price.get();
        }
    }; 


    public TestOnBindings(){
        total.bind(totalBinding); //should really not do that, looks ugly
        quantity.bind(quantityBinding); //now you're asking for troubles
    }


    public void setPrice(Double price){
        this.price.set(price);
    }


    public void setQuantity(Double quantity){
        this.quantity.set(quantity);
    }

    public void setTotal(Double total){
        this.total.set(total);
    }


    public Double getTotal(){
        return total.get();
    }


    public Double getQuantity(){
        return quantity.get();
    }

    public static void main(String[] args) {
        TestOnBindings test = new TestOnBindings();
        test.setQuantity(5.0);

        System.out.println("Total amount = " + test.getTotal());
    }

}

和明显的错误:

线程中的异常&#34; main&#34; java.lang.RuntimeException:无法设置绑定值。     在javafx.beans.property.DoublePropertyBase.set(DoublePropertyBase.java:142)

2 个答案:

答案 0 :(得分:2)

你真的必须使用绑定吗?这将完成这项工作。无论如何,绑定只是花哨的听众。

public TestOnBindings(){
    price.addListener((obs,ov,nv) -> {
        total.set(price.doubleValue()*quantity.doubleValue());
    });
    //could be same listener as price if total is a complicated function
    quantity.addListener((obs,ov,nv) -> {
        total.set(price.doubleValue()*quantity.doubleValue());
    });
    total.addListener((obs,ov,nv) -> {
        quantity.set(total.doubleValue()/price.doubleValue());
    });
}

答案 1 :(得分:2)

我认为我的问题已经在另一个问题post中得到了解答,并且这个人在blog上提供了一个简单的方法

我修改了他的课程以适应我的需要,并且有一个非常容易使用的课程:

public class CustomBinding {

    public static <A ,B> void bindBidirectional(Property<A> propertyA, Property<B> propertyB, Function<A,B> updateB, Function<B,A> updateA){
        addFlaggedChangeListener(propertyA, propertyB, updateB);
        addFlaggedChangeListener(propertyB, propertyA, updateA);
    }

    public static <A ,B> void bind(Property<A> propertyA, Property<B> propertyB, Function<A,B> updateB){
        addFlaggedChangeListener(propertyA, propertyB, updateB);
    }

    private static <X,Y> void addFlaggedChangeListener(ObservableValue<X> propertyX, WritableValue<Y> propertyY, Function<X,Y> updateY){
        propertyX.addListener(new ChangeListener<X>() {
            private boolean alreadyCalled = false;

            @Override
            public void changed(ObservableValue<? extends X> observable, X oldValue, X newValue) {
                if(alreadyCalled) return;
                try {
                    alreadyCalled = true;
                    propertyY.setValue(updateY.apply(newValue));
                }
                finally {alreadyCalled = false; }
            }
        });
    }
}

然后将它应用到我的示例中......仍然需要进行一些微调,但它可以完成这项任务。

public class TestOnBindings {

    private DoubleProperty price = new SimpleDoubleProperty(10.0);
    private DoubleProperty quantity = new SimpleDoubleProperty(1.0);
    private DoubleProperty total = new SimpleDoubleProperty(1.0);

    public TestOnBindings(){
        CustomBinding.<Number,Number>bindBidirectional(quantity, total, 
                (newQuantity)-> newQuantity.doubleValue() * price.get(),
                (newTotal)-> newTotal.doubleValue() /price.get());

        CustomBinding.<Number,Number>bind(price, total, 
                (newPrice)-> newPrice.doubleValue() * quantity.get());
    }

    public void setPrice(Double price){this.price.set(price);}
    public void setQuantity(Double quantity){this.quantity.set(quantity);}
    public void setTotal(Double total){this.total.set(total);}

    public Double getTotal(){return total.get();}
    public Double getQuantity(){return quantity.get();}
    public Double getPrice(){return price.get();}


    public static void main(String[] args) {
        TestOnBindings test = new TestOnBindings();

        test.setQuantity(5.0);

        System.out.println("Quantity = " + test.getQuantity());
        System.out.println("Price = " + test.getPrice());
        System.out.println("Total = " + test.getTotal());

        test.setTotal(60.0);

        System.out.println("---------------------------------------------");
        System.out.println("Quantity = " + test.getQuantity());
        System.out.println("Price = " + test.getPrice());
        System.out.println("Total = " + test.getTotal());

        test.setPrice(5.0);

        System.out.println("---------------------------------------------");
        System.out.println("Quantity = " + test.getQuantity());
        System.out.println("Price = " + test.getPrice());
        System.out.println("Total = " + test.getTotal());
    }

}