아마도 JavaFX 바인딩을 잘못 이해했거나 SimpleStringProperty에 버그가 있습니다.JavaFX 2.0에서의 바인딩
이 테스트 코드를 실행할 때 변경된 모델 값이 새 값을 얻지 못했습니다. 테스트 testBindingToModel이 실패합니다. 내 모델이 TextField tf의 값으로 업데이트되어야한다고 생각했습니다. 그러나 prop1Binding의 바인딩 값은 "test"값을 가져옵니다.
public class BindingTest {
private TextField tf;
private Model model;
private ModelBinding mb;
@Before
public void prepare() {
tf = new TextField();
model = new Model();
mb = new ModelBinding(model);
Bindings.bindBidirectional(tf.textProperty(), mb.prop1Binding);
}
@Test
public void testBindingToMB() {
tf.setText("test");
assertEquals(tf.getText(), mb.prop1Binding.get());
}
@Test
public void testBindingToModel() {
tf.setText("test");
assertEquals(tf.getText(), mb.prop1Binding.get());
assertEquals(tf.getText(), model.getProp1());
}
private static class ModelBinding {
private final StringProperty prop1Binding;
public ModelBinding(Model model) {
prop1Binding = new SimpleStringProperty(model, "prop1");
}
}
private static class Model {
private String prop1;
public String getProp1() {
return prop1;
}
public void setProp1(String prop1) {
this.prop1 = prop1;
}
}
}
도움 주셔서 감사합니다.
안부 세바스찬
편집 : 내가 직접 모델의 값을 설정할 수 있습니다이 클래스와 . 나는이 수업을 다음 날 시험 할 것이고 나의 결과와 함께이 게시물에 대해 의견을 말할 것이다.
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
}
}
}
}
안녕하세요, 답변 해 주셔서 감사합니다. 그들은 직접 값을 설정하는 옵션을 잊어 버렸습니까? 예를 들어 내 모델을 JavaFX Properties와 섞어 놓고 싶지 않았습니다. 문제 해결을위한 제 질문으로 문제가 해결되어 문제가 해결되었습니다. 내 모델을 직접 바인딩하려는 경우 더 고려해야합니까? – McPepper