2012-08-06 127 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