JavaFx:双向绑定数字,带加/乘/ [英] JavaFx: Bind number bidirectional with add/multiply/

查看:295
本文介绍了JavaFx:双向绑定数字,带加/乘/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想双向绑定两个DoubleProperty,但不是真正的1:1,例如以1:1.2的比例绑定.

I want to bind two DoubleProperty bidirectional but not really 1:1, but for example in 1:1.2 proportion.

我需要类似的东西:

DoubleProperty x;
DoubleProperty y;

x.bindBidirectional(y.multiply(1.2));

因此,每次设置 x 的值时, y 值应为 x * 1.2 并且每次设置 y 值时,x均应为 y/1.2

So every time the value of x is set, the y value should be x*1.2 and every time the y value is set the x should be y/1.2

我该怎么办?

推荐答案

Afaik已经不存在,因此您需要自己添加类似内容.

Afaik there is nothing already in existance so you need to inplement something like this yourself.

public abstract class BidirectionalBinding<S, T> {

    protected final Property<S> property1;
    protected final Property<T> property2;

    protected boolean calculating = false;
    private final InvalidationListener listener;

    /**
     * Convert value for property 1 to value for property 2
     * 
     * @param value
     * @return
     */
    protected abstract T convert(S value);

    /**
     * Convert value for property 2 to value for property 1
     * 
     * @param value
     * @return
     */
    protected abstract S inverseConvert(T value);

    protected BidirectionalBinding(Property<S> property1, Property<T> property2) {
        if (property2.isBound() || property1 == null) {
            throw new IllegalArgumentException();
        }

        this.property1 = property1;
        this.property2 = property2;
        property2.setValue(convert(property1.getValue()));

        listener = o -> {
            if (!calculating) {
                calculating = true;
                try {
                    if (o == property1) {
                        T value = convert(property1.getValue());
                        property2.setValue(value);
                    } else {
                        S value = inverseConvert(property2.getValue());
                        property1.setValue(value);
                    }
                } catch (Exception ex) {
                    dispose();
                }
                calculating = false;
            }
        };

        property1.addListener(listener);
        property2.addListener(listener);
    }

    public void dispose() {
        property1.removeListener(listener);
        property2.removeListener(listener);
    }
}

DoubleProperty x = new SimpleDoubleProperty(3);
DoubleProperty y = new SimpleDoubleProperty();
final double factor = 1.2;

BidirectionalBinding<Number, Number> binding = new BidirectionalBinding<>(x, y) {

    @Override
    protected Number convert(Number value) {
        return value.doubleValue() * factor;
    }

    @Override
    protected Number inverseConvert(Number value) {
        return value.doubleValue() / factor;
    }

};

System.out.printf("x = %f; y = %f\n", x.get(), y.get());
x.set(5);
System.out.printf("x = %f; y = %f\n", x.get(), y.get());
y.set(15);
System.out.printf("x = %f; y = %f\n", x.get(), y.get());

请注意,此实现是通用的.如果要处理特殊属性,则可能需要修改代码以使用基本类型,以避免转换为包装器类型...

Note that this implementation is generalized. If you're dealing with specialized properties, you may want to modify the code to use the primitive types to avoid the conversion to wrapper types...

这篇关于JavaFx:双向绑定数字,带加/乘/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆