如何从DoubleBinding创建BigDecimal

时间:2019-05-15 09:45:56

标签: javafx

如何从BigDecimal创建DoubleBinding

private transient ObjectProperty<BigDecimal> sell;

private ObjectProperty<Operation> operation = new SimpleObjectProperty<>();
private ObjectProperty<BigDecimal> volume = new SimpleObjectProperty<>();

有错误:

 sell = Bindings.createDoubleBinding(new Callable<Double>() {
                Double volumeDouble = volume.get().doubleValue();
                @Override
                public Double call() throws Exception {
                    return (operation.get() == Operation.SELL) ? volumeDouble : 0;
                }
            }, volume, operation);

1 个答案:

答案 0 :(得分:2)

您不能将ObjectBinding<BigDecimal>强制转换为ObjectProperty<BigDecimal>,因为在这种情况下使用的绑定类不会扩展ObjectProperty。您可以将SimpleObjectProperty<BigDecimal>绑定到ObjectBinding<BigDecimal>坚韧。

顺便说一句:请注意,代码段中的volumeDouble字段是在创建绑定时分配的,即使以后修改volume属性,也不会基于卷进行更新。 / p>

final ObjectProperty<BigDecimal> sell = new SimpleObjectProperty<>(); 
ObjectBinding<BigDecimal> binding = Bindings.createObjectBinding(new  Callable<BigDecimal>() {
        @Override
        public BigDecimal call() {
            return (operation.get() == Operation.SELL) ? volume.get() : BigDecimal.ZERO;
        }
    }, volume, operation);
sell.bind(binding);