2

javafx 用の動的ビュー生成ユーティリティの作成に苦労しています。ObjectProperty または StringProperty を持つクラスがいくつかあります。プロパティごとに ComboBox を作成し、可能であればコンボ選択した値を名前で Class プロパティに直接バインドしたいと考えています。javafx.beans.binding のいずれかに、オブジェクトと文字列名を指定してプロパティを取得できるヘルパーまたはメソッドはありますか。または、プロパティのリストを取得するだけです。文字列を取得して名前でプロパティに一致させるメソッドがありますが、オブジェクトの各プロパティにケースが必要です。これは、20以上のプロパティを持つオブジェクトでは多くの重複コードです。

戻り値の型として javafx.bean.property を探していると指定すると思います。

4

2 に答える 2

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(); }
}
于 2012-08-06T20:08:48.213 に答える
0

apache commonsbeanutilsをチェックしてください

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

于 2012-08-06T19:40:47.357 に答える