8

我只是试图绑定一个 Integer 和一个 String 属性。经过一番谷歌搜索后,这应该可以使用提供的两种方法之一来实现:

  1. 公共静态无效绑定双向(属性stringProperty,
    属性otherProperty,StringConverter转换器)

  2. public static void bindBidirectional(Property stringProperty,
    Property 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);    
    }
}
4

4 回答 4

17

类型混淆的简单问题

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

应该:

Bindings.bindBidirectional(textProp, intProp, new NumberStringConverter());
于 2014-05-06T14:39:58.500 回答
3

我在 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);    
    }
}

输出是:

字符串属性 [值:2]

整数属性 [值:8]

于 2013-01-03T12:27:20.690 回答
3

我有一个类似的问题。我试图将字符串转换为文件对象并返回。但我使用了 Bindings.bindBidirectional(...,...,java.text.Format)。从字符串到文件的转换按预期工作,但在另一个方向上结果为空。我用你的例子试过了,同样的结果!我认为绑定机制存在错误,或者我对 java.text.Format 的实现是错误的..

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

让事情按预期工作的唯一方法是按照 Hendrik Ebbers 的建议实施 StringConverter。谢谢你的提示!

于 2013-03-25T13:48:35.390 回答
0

我认为这是一个错误。无论如何,您可以解决以下问题:

StringConverter sc = new IntegerStringConverter();
Bindings.bindBidirectional(textProp, intProp, sc);
于 2013-01-03T12:10:54.420 回答