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.

5/30/2014 1:56:59 AM

Accepted Answer

I'm not sure but have a look at BeanPathAdapter

Its part of JFXtras-Labs, downloadable jar.

Source is here on GitHub.

5/30/2014 4:06:46 PM

How about adapters for the POJOs?


public class Person{
  private String name;
  private Address addr;

  \\getters, setters...

And for the JavaFX GUI

public class FXPerson{
  public FXPerson(Person p){ = \\build StringProperty
    this.fxaddr = \\build ObjectProperty<FXAddress>

  private StringProperty name;
  private ObjectProperty<FXAddress> fxaddr;

Downside: For every POJO you will have to write an adapter. And if a POJO changes (e.g. new property etc.) you will need to update the corresponding adapter.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow