JavaFX2.0中的绑定

JavaFX2.0中的绑定,java,data-binding,javafx-2,javafx,Java,Data Binding,Javafx 2,Javafx,也许我误解了JavaFX绑定,或者SimpleStringProperty中有一个bug 当我运行这个测试代码时,我更改的模型值没有得到新的值。testBindingToModel测试失败。我认为我的模型应该用TextField的值更新。但只有prop1Binding的绑定值才能获得值“test” 谢谢你的帮助 致意 塞巴斯蒂安 编辑: 有了这个类,我可以直接设置模型的值。我将在接下来的几天里测试这门课,并用我的结果评论这篇文章 public class MySimpleStringProper

也许我误解了JavaFX绑定,或者SimpleStringProperty中有一个bug

当我运行这个测试代码时,我更改的模型值没有得到新的值。testBindingToModel测试失败。我认为我的模型应该用TextField的值更新。但只有prop1Binding的绑定值才能获得值“test”

谢谢你的帮助

致意 塞巴斯蒂安

编辑: 有了这个类,我可以直接设置模型的值。我将在接下来的几天里测试这门课,并用我的结果评论这篇文章

public class MySimpleStringProperty extends SimpleStringProperty {
    public MySimpleStringProperty(Object obj, String name) {
        super(obj, name);
    }

    public MySimpleStringProperty(Object obj, String name, String initVal) {
        super(obj, name, initVal);
    }

    @Override
    public void set(String arg0) {
        super.set(arg0);
        if (this.getBean() != null) {
            try {
                Field f = this.getBean().getClass().getDeclaredField(this.getName());
                f.setAccessible(true);
                f.set(this.getBean(), arg0);
            } catch (NoSuchFieldException e) {
                // logging here
            } catch (SecurityException e) {
                // logging here
            } catch (IllegalArgumentException e) {
                // logging here
            } catch (IllegalAccessException e) {
                // logging here
            }
        }
    }
}

不幸的是,此构造函数没有将
SimpleStringProperty
附加到bean对象。它只是告诉
SimpleStringProperty
哪个bean属性属于哪个bean

例如,如果你想在你的类中拥有一个属性,你应该下一步做:

public static class Model {

    private StringProperty prop1 = 
          new SimpleStringProperty(this, "prop1", "default_value");

    public String getProp1() {
        return prop1.get();
    }

    public void setProp1(String value) {
        prop1.set(value);
    }

    public StringProperty prop1Property() {
        return prop1;
    }
}

请注意,无法绑定到原始的
模型
类,因为它不提供有关设置新
prop1
值的事件。如果您想拥有可观察的模型,您应该从一开始就使用fx属性。

不幸的是,此构造函数没有将
SimpleStringProperty
附加到bean对象。它只是告诉
SimpleStringProperty
哪个bean属性属于哪个bean

例如,如果你想在你的类中拥有一个属性,你应该下一步做:

public static class Model {

    private StringProperty prop1 = 
          new SimpleStringProperty(this, "prop1", "default_value");

    public String getProp1() {
        return prop1.get();
    }

    public void setProp1(String value) {
        prop1.set(value);
    }

    public StringProperty prop1Property() {
        return prop1;
    }
}

请注意,无法绑定到原始的
模型
类,因为它不提供有关设置新
prop1
值的事件。如果你想有一个可观察的模型,你应该从一开始就使用fx属性。

刚刚发现提供了JavaBeanStringProperty类,它正好满足了我的要求

使用这段代码,我可以直接将bean的值绑定到StringProperty(包括设置和从bean获取我的值)


我发现的唯一问题是,当您在设置绑定后更改模型的值时,没有更新,例如在TextField中。

刚刚发现提供了JavaBeanStringProperty类,它正好满足了我的请求

使用这段代码,我可以直接将bean的值绑定到StringProperty(包括设置和从bean获取我的值)


我发现的唯一问题是,当您在设置绑定后更改模型的值时,例如文本字段中没有更新。

Hi,谢谢您的回答。他们是否忘记了直接设置值的选项?例如,我不想让我的模型与JavaFX属性混淆。我更新了我的问题,并对问题进行了修复,这为我解决了问题。如果我想直接绑定我的模型,还需要考虑更多吗?嗨,谢谢你的回答。他们是否忘记了直接设置值的选项?例如,我不想让我的模型与JavaFX属性混淆。我更新了我的问题,并对问题进行了修复,这为我解决了问题。如果我想直接绑定我的模型,还有什么要考虑的吗?
binding = JavaBeanStringPropertyBuilder.create().beanClass(Model.class).bean(model).name("prop1").build();