JavaFX绑定:BeanPathAdapter是否工作? [英] JavaFX binding: does BeanPathAdapter work?

查看:238
本文介绍了JavaFX绑定:BeanPathAdapter是否工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用于DTO POJOS。而对于桌面客户端GUI,我使用JavaFX。当然,我想要双向(非mono!)数据绑定。我找到了两个解决方案:


1)使用特殊的类适配器。例如,如果我们有POJO类Person,那么我们创建JavaFx(*属性)类PersonAdapter。除了POJO人员,我们还添加了PropertyChangeSupport。这个方法是有效的,但每个DTO写DTOAdapter都是必要的。 - 不好。


2)我发现这篇文章 https://ugate.wordpress.com/2012/06/14/javafx-programmatic-pojo-bindings/ ,此代码为javafx8 https://github.com/JFXtras/jfxtras-labs /blob/8.0/src/main/java/jfxtras/labs/scene/control/BeanPathAdapter.java 。说实话我不明白他们会如何看出POJO是否改变了。换句话说,如果javafx更改 - POJO更改清楚。但是如果POJO改变了?一些神秘的我想,并试图测试

  class Person {
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}

测试代码:

  Person person = new Person(); 
SimpleStringProperty sp = new SimpleStringProperty();
BeanPathAdapter< Person> testPA = new BeanPathAdapter(person);
testPA.bindBidirectional(name,sp);
person.setName(Name1);
System.out.println(Test1:+ sp.get());
sp.set(Name2);
System.out.println(Test2:+ test.getName()

这里结果是:

  Test1:null 
Test2:Name2
/ pre>

所以我们看到方向javafx属性 - > pojo工作,但是pojo-> javafx属性不会,或者我做错了,可能有人会建议一个更好的解决方案?

解决方案


不幸的是,它没有工作bindBidirectional不能修改bean来监听它的变化,你可以混淆名称bindBidirectional,这可以解释为它绑定双向属性(在你的例子中)和 BeanPathAdapter 内部属性javafx - > adapter并且还包装bean)和适配器 - > javafx在您的条款。



所以,bean的更改不会影响适配器和绑定的属性,但是我们如何可以更改适配器没有更改属性和bean?通过更改绑定到同一个bean的另一个属性属性,例如:

  Person person = new Person(); 

SimpleStringProperty firstProperty = new SimpleStringProperty();
SimpleStringProperty secondProperty = new SimpleStringProperty();

BeanPathAdapter< Person> testPA = new BeanPathAdapter(person);

testPA.bindBidirectional(name,firstProperty);
testPA.bindBidirectional(name,secondProperty);

firstProperty.set(fromFirst);
System.out.println(first:+ firstProperty.get()+second =+ secondProperty.get()+bean =+ person.getName());
secondProperty.set(fromSecond);
System.out.println(first:+ firstProperty.get()+second =+ secondProperty.get()+bean =+ person.getName());

person.setName(fromBean);
System.out.println(first:+ firstProperty.get()+second =+ secondProperty.get()+bean =+ person.getName());

结果如下:

  first:fromFirst second = fromFirst bean = fromFirst 
first:fromSecond second = fromSecond bean = fromSecond
first:fromSecond second = fromSecond bean = fromBean但是你可以通过添加testPA.setBean(person)强制更新属性,但是你可以通过添加testPA.setBean(person)来强制更新属性。在您要处理的bean的每次更改后。


I use for DTO POJOS. And for desktop client GUI I use JavaFX. And of course, I would like to have bidirectional (not mono!) data binding. I've found two solutions:

1) to use special classes Adapters. For example if we have POJO class Person, then we create JavaFx (*property) class PersonAdapter. Besides in POJO Person we add PropertyChangeSupport. This method works, however it's necessary for every DTO write DTOAdapter. - bad.

2)I've found this article https://ugate.wordpress.com/2012/06/14/javafx-programmatic-pojo-bindings/ and this code for javafx8 https://github.com/JFXtras/jfxtras-labs/blob/8.0/src/main/java/jfxtras/labs/scene/control/BeanPathAdapter.java . To tell the truth I didn't understand how they will find out if the POJO is changed. By other words if javafx changes - POJO changes it's clear. But if POJO changes? Some mystery I thought and desided to test

class Person{
 private String name;
 public void setName(String name){
 this.name=name;
 }
 public String getName(){
  return this.name;
  }
}

And the testing code:

Person person=new Person();
SimpleStringProperty sp=new SimpleStringProperty();
BeanPathAdapter<Person> testPA = new BeanPathAdapter<>(person);
testPA.bindBidirectional("name", sp);
person.setName("Name1");
System.out.println("Test1:"+sp.get());
sp.set("Name2");
System.out.println("Test2:"+test.getName()

Here is the result:

Test1:null
Test2:Name2

So we see that direction javafx property -> pojo works. However, pojo->javafx property doesn't. Or I do something wrong, or it doesn't work? Maybe someone will sugest a better solution?

解决方案

Unfortunately, it doest't work that. bindBidirectional doesn't modify bean for listening it changes. You can be confused with name bindBidirectional. That can be explained as it binds bidirectionally property (sp in your example) and BeanPathAdapter inner property. javafx -> adapter (and also wrapped bean) and adapter -> javafx in your terms.

So, changes of bean doesn't effects adapter and binded properties. But how we can change adapter without changing property and bean? By changing another property binded to same bean property. For example:

    Person person = new Person();

    SimpleStringProperty firstProperty = new SimpleStringProperty();
    SimpleStringProperty secondProperty = new SimpleStringProperty();

    BeanPathAdapter<Person> testPA = new BeanPathAdapter<>(person);

    testPA.bindBidirectional("name", firstProperty);
    testPA.bindBidirectional("name", secondProperty);

    firstProperty.set("fromFirst");
    System.out.println("first:" + firstProperty.get() + " second=" + secondProperty.get() + " bean=" + person.getName());
    secondProperty.set("fromSecond");
    System.out.println("first:" + firstProperty.get() + " second=" + secondProperty.get() + " bean=" + person.getName());

    person.setName("fromBean");
    System.out.println("first:" + firstProperty.get() + " second=" + secondProperty.get() + " bean=" + person.getName());

Here is the result:

    first:fromFirst second=fromFirst bean=fromFirst
    first:fromSecond second=fromSecond bean=fromSecond
    first:fromSecond second=fromSecond bean=fromBean

But you can force update properties by adding testPA.setBean(person); after each changes of bean which you want to handle.

这篇关于JavaFX绑定:BeanPathAdapter是否工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆