按名称检索属性

时间:2012-08-06 19:37:03

标签: reflection binding javafx-2

我正在努力为javafx创建动态视图生成实用程序。我有一些具有ObjectProperty或StringProperty的类我想为每个属性创建一个ComboBox,并且如果可能的话,直接将组合选择的值绑定到Class属性。在任何javafx.beans.binding中是否有一些帮助器或方法允许我指定一个Object和一个String名称并检索该属性。或者只是检索属性列表。我现在有一个方法,它接受字符串并通过名称将其匹配到属性,但它要求我对对象上的每个属性都有一个大小写,在具有20多个属性的对象上有很多重复的代码。

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

2 个答案:

答案 0 :(得分: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(); }
}

答案 1 :(得分:0)

查看apache commons bean 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

那里有很多其他有用的方法,对于UI工作,它们特别方便,因为它们中的许多都返回了你想要显示的字符串形式。

如果您想要对象而不是字符串,请使用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