Search code examples
javapropertiesjavafxjavafx-8pojo

Convert POJO to JavaFX Property


In my project, I have a number of POJOs whose fields are shown in a JavaFX GUI. I currently update the UI using a manual refresh – whenever the POJO changes, I call a refresh() method. I would like to attempt using binding to accomplish this instead, which requires using JavaFX properties. Whilst I can change the object, its internal fields are other objects, which I cannot change (they are populated using GSON, which AFAIK uses 'normal' Java objects – e.g. String, not StringProperty). Furthermore, the object is read only – it only has getters, not setters.

I believe I can use a ReadOnlyJavaBeanObjectPropertyBuilder (yay Java naming?) or a ReadOnlyObjectWrapper to wrap the object as a property. However, the internal fields – which are what I want to bind the Labels to – are not converted to properties. Is there any way of doing such a recursive conversion – convert an entire object which contains normal object fields into a property which contains further properties? Is this even necessary – am I doing something wrong?

EDIT: I suspect any solution would have to use reflection.


Solution

  • I'm not sure but have a look at BeanPathAdapter

    Its part of JFXtras-Labs, downloadable jar.

    Source is here on GitHub.