不同性质的双向绑定(Bidirectional bindings of different prop

2019-07-17 19:13发布

我只是想绑定一个整数和字符串属性。 一些谷歌上搜索后,这应该是可能与提供的两个方法之一:

  1. 公共静态无效bindBidirectional(物业stringProperty,
    物业otherProperty,转换字符串转换)

  2. 公共静态无效bindBidirectional(物业stringProperty,
    物业otherProperty,java.text.Format子格式)

不幸这似乎并没有为我工作。 我究竟做错了什么?

import java.text.Format;

import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.util.converter.IntegerStringConverter;

public class BiderectionalBinding {

    public static void main(String[] args) {
        SimpleIntegerProperty intProp = new SimpleIntegerProperty();
        SimpleStringProperty textProp = new SimpleStringProperty();

        Bindings.bindBidirectional(textProp, intProp, new IntegerStringConverter());

        intProp.set(2);
        System.out.println(textProp);

        textProp.set("8");
        System.out.println(intProp);    
    }
}

Answer 1:

型号混乱的简单的事情

Bindings.bindBidirectional(textProp, intProp, new IntegerStringConverter());

应该:

Bindings.bindBidirectional(textProp, intProp, new NumberStringConverter());


Answer 2:

我想你的代码在Eclipse中,不得不投的转换器。 然后一切正常:

public class BiderectionalBinding {

    public static void main(String[] args) {
        SimpleIntegerProperty intProp = new SimpleIntegerProperty();
        SimpleStringProperty textProp = new SimpleStringProperty();
        StringConverter<? extends Number> converter =  new IntegerStringConverter();

        Bindings.bindBidirectional(textProp, intProp,  (StringConverter<Number>)converter);

        intProp.set(2);
        System.out.println(textProp);

        textProp.set("8");
        System.out.println(intProp);    
    }
}

输出是:

StringProperty [值:2]

IntegerProperty [值:8]



Answer 3:

i had a similar problem. I tried to convert a string into a File-Object and back. But i used Bindings.bindBidirectional(...,...,java.text.Format). The conversion from the string to file worked as expected but in the other direction the result was null. I tried it with your example, same Result! I think there is a bug in the binding mechanism, or maybe my implementation of java.text.Format is wrong..

package de.ludwig.binding.model;

import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;

import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class BidirectionalBinding {

    public static void main(String[] args) {
        SimpleIntegerProperty intProp = new SimpleIntegerProperty();
        SimpleStringProperty textProp = new SimpleStringProperty();

        Bindings.bindBidirectional(textProp, intProp, new Format() {

            @Override
            public StringBuffer format(Object obj, StringBuffer toAppendTo,
                    FieldPosition pos) {
                return toAppendTo.append(obj.toString());
            }

            @Override
            public Object parseObject(String source, ParsePosition pos) {
                return Integer.parseInt(source);
            }

        });

        intProp.set(2);
        System.out.println(textProp);

        textProp.set("8");
        System.out.println(intProp);    
    }
}

The only way to get things working as expected was to implement StringConverter as recommended by Hendrik Ebbers. Thank you for this tip!



Answer 4:

我认为这是一个错误。 反正你可以像解决方法:

StringConverter sc = new IntegerStringConverter();
Bindings.bindBidirectional(textProp, intProp, sc);


文章来源: Bidirectional bindings of different properties