2012-08-06 122 views
2

我努力为javafx创建一个动态视图生成实用程序。我有几个具有ObjectProperty或StringProperty的类我想为每个属性创建一个ComboBox,并在可能的情况下直接将组合选定值绑定到Class属性。在javafx.beans.binding中是否有一些帮助器或方法允许我指定Object和String名称并检索属性。或者只是检索一个属性列表。我现在有一个方法,它接受字符串并通过名称将其与属性相匹配,但它要求我有一个对象上的每个属性的情况,这对于具有20+属性的对象来说是很多重复的代码。按名称检索属性

我想指定我正在寻找javafx.bean.property作为返​​回类型。

回答

2

你总是可以使用Java Reflection

获取的属性列表

for (Method method : Node.class.getMethods()) { 
    String name = method.getName(); 
    if (name.endsWith("Property")) { 
     Type returnType = method.getReturnType(); 
     String propName = name.replace("Property", ""); 
     System.out.println(propName + " : " + returnType); 
    } 
} 

下面是绑定和例如反射方法:

public class ReflectiveBind extends Application { 
    /** 
    * Reflection call for code like 
    * slider1.valueProperty().bindBidirectional(slider2.valueProperty()); 
    * 
    * @param bindee Node which you want to be changed by binding 
    * @param propertyName name of the property, e.g. width 
    * @param bindTarget Node which you want to be updated by binding 
    */ 
    private static void bind(Object bindee, String propertyName, Object bindTarget) throws Exception { 
     // here we get slider1.valueProperty() 
     Method methodForBindee = bindee.getClass().getMethod(propertyName + "Property", (Class[]) null); 
     Object bindableObj = methodForBindee.invoke(bindee); 

     // here we get slider2.valueProperty() 
     Method methodForBindTarget = bindTarget.getClass().getMethod(propertyName + "Property", (Class[]) null); 
     Object bindTargetObj = methodForBindTarget.invoke(bindTarget); 

     // here we call bindBidirectional: slider1.valueProperty().bindBidirectional(slider2.valueProperty()) 
     Method bindMethod = bindableObj.getClass().getMethod("bindBidirectional", Property.class); 
     bindMethod.invoke(bindableObj, bindTargetObj); 
    } 

    @Override 
    public void start(Stage stage) { 

     Slider slider1 = new Slider(); 
     Slider slider2 = new Slider(); 

     VBox root = new VBox(20); 
     root.getChildren().addAll(slider1, slider2); 

     stage.setScene(new Scene(root, 200, 100)); 
     stage.show(); 

     try { 
      //same call as slider1.valueProperty().bindBidirectional(slider2.valueProperty()); 
      bind(slider1, "value", slider2); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { launch(); } 
} 
+0

绑定的方法看起来不错。我尝试过,但遇到了ClastCastException,也是它的双向。我还没有完全剖析整个逻辑,所以我不确定我可以做些什么来适应我的需要。 – Fozz 2012-08-06 20:40:22

+0

我已经改变了调用双向绑定的方法,添加了javadoc并提供了一个演示应用的应用程序。 – 2012-08-07 10:06:26

0

退房阿帕奇百科全书豆utils的

http://commons.apache.org/beanutils/

你说你要...

获取属性的值: http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/BeanUtils.html#getProperty%28java.lang.Object,%20java.lang.String%29

获取属性列表: http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/BeanUtils.html#describe%28java.lang.Object%29

很多其他有用的方法,对于用户界面的工作,他们特别方便,因为他们中的许多人返回字符串形式,这是你想要显示。

如果你想对象而不是字符串使用PropertUtils类,而不是

取得属性(而不是作为一个字符串)属性 http://commons.apache.org/beanutils/v1.8.3/apidocs/org/apache/commons/beanutils/PropertyUtils.html#getProperty%28java.lang.Object,%20java.lang.String%29

获取列表的值: http://commons.apache.org/beanutils/v1.8.3/apidocs/org/apache/commons/beanutils/PropertyUtils.html#describe%28java.lang.Object%29

+0

除非有更多的方法,你连接的两个不来接近我”米寻找他们返回的字符串,而不是属性。返回将是javafx.bean.property。 – Fozz 2012-08-06 20:05:48