2012-10-04 4 views
1

아마도 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 
      } 
     } 
    } 
} 

답변

2

그냥 JavaBeanStringProperty 클래스가 제공된다는 것을 알았습니다.이 클래스는 내 요청을 충만히 완료합니다.

이 코드를 사용하여 Bean 값을 StringProperty에 직접 바인딩 할 수 있습니다 (설정을 포함하고 Bean에서 /로 내 값 가져 오기 포함).

binding = JavaBeanStringPropertyBuilder.create().beanClass(Model.class).bean(model).name("prop1").build(); 

유일한 문제점은 바인딩을 설정 한 후에 모델 값을 변경하면 업데이트가 없다는 것입니다. TextField에서.

2

이 생성자는 불행히도 SimpleStringProperty을 bean 객체에 첨부하지 않습니다. 어느 bean 속성에 속하는지는 SimpleStringProperty입니다.

예, 당신은 당신이 그것을 다음 방법으로해야 클래스의 속성 갖고 싶어 : 그것에 대해 어떤 이벤트를 제공하지 않기 때문에 원래 Model 클래스에 결합 할 수있는 방법이 없다는 것을,

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 속성을 사용해야합니다.

+0

안녕하세요, 답변 해 주셔서 감사합니다. 그들은 직접 값을 설정하는 옵션을 잊어 버렸습니까? 예를 들어 내 모델을 JavaFX Properties와 섞어 놓고 싶지 않았습니다. 문제 해결을위한 제 질문으로 문제가 해결되어 문제가 해결되었습니다. 내 모델을 직접 바인딩하려는 경우 더 고려해야합니까? – McPepper