Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 862
omor1 Avatar asked Sep 13 '25 11:09

omor1


2 Answers

I'm not sure but have a look at BeanPathAdapter

Its part of JFXtras-Labs, downloadable jar.

Source is here on GitHub.

like image 133
Jurgen Avatar answered Sep 15 '25 01:09

Jurgen


How about adapters for the POJOs?

Example

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

  \\getters, setters...
}

And for the JavaFX GUI

public class FXPerson{
  public FXPerson(Person p){
    this.name = \\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.

like image 29
FuryFart Avatar answered Sep 15 '25 00:09

FuryFart