lundi 9 janvier 2017

JavaFx: Make observable clones

I have a Model class that I want to keep it's fields simple (i.e. not properties) to be able to use them in other platform developments. But I want to bind those fields to TextFields, What I thought of, is to create a special copy of this class and have the copy contain all of the original class fields as properties. What I did is this:

public static <T> ObjectProperty<T> createObservableClone(T object){
    ObjectProperty<T> observableObject = new SimpleObjectProperty<T>();

    for(Field field : object.getClass().getFields()){
        //I think this would create the property out of the original fields
        ObjectProperty property = new SimpleObjectProperty(object,field.getName());

        //What to do here
    }

    return observableObject;
}

I used reflection to create the property out of the field, but how do I add this property dynamically to observableObject?

Otherwise if this approach is wrong what are the recommended one to do this?





Aucun commentaire:

Enregistrer un commentaire