不同属性的双向绑定

det*_*ef1 6 java binding properties bidirectional javafx-2

我只是试图绑定一个Integer和一个String属性.经过一些谷歌搜索后,应该可以使用以下两种方法之一:

  1. public static void bindBidirectional(Property stringProperty,
    Property otherProperty ,StringConverter converter)

  2. public static void bindBidirectional(Property stringProperty,
    Property otherProperty ,java.text.Format 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);    
    }
}
Run Code Online (Sandbox Code Playgroud)

Emi*_* L. 17

简单的类型混淆问题

Bindings.bindBidirectional(textProp, intProp, new IntegerStringConverter());
Run Code Online (Sandbox Code Playgroud)

应该:

Bindings.bindBidirectional(textProp, intProp, new NumberStringConverter());
Run Code Online (Sandbox Code Playgroud)


Ulu*_*Biy 0

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

StringConverter sc = new IntegerStringConverter();
Bindings.bindBidirectional(textProp, intProp, sc);
Run Code Online (Sandbox Code Playgroud)