Base
I have a mysql DB managed by JPA (EclipseLink) (Entities and Controllers + persistence unit). The GUI is JavaFX based.
Informat
I would generally advocate using JavaFX properties in JPA entities: I really see no obvious reason not to do so.
However, if you want to avoid doing so, you can use JavaBeanPropertyAdapters. These are adapters that create JavaFX observable properties wrapping regular JavaBean properties. So if you have a bean class
@Entity
public class Person {
private String firstName ;
private String lastName ;
@Id
private Integer id ;
public String getFirstName() {
return firstName ;
}
public void setFirstName(String firstName) {
this.firstName = firstName ;
}
public String getLastName() {
return lastName ;
}
public void setLastName(String lastName) {
this.lastName = lastName ;
}
}
Then you can do something like
TableColumn firstNameCol = new TableColumn<>("First Name");
firstNameCol.setCellValueFactory(cellData -> {
try {
return JavaBeanStringPropertyBuilder.create()
.bean(cellData.getValue())
.name("firstName")
.build();
} catch (NoSuchMethodException exc) {
throw new RuntimeException(exc);
}
});
This will create a JavaFX property for use in the table, and unidirectionally bind the JavaBean property to it: i.e. if you change the value in the table, the JavaBean will be updated. The reverse binding will not occur with this set up, i.e. changing the value in the bean will not update the value displayed in the table.
If you want bidirectional binding, your bean will need to support property change listeners:
public class Person {
private String firstName ;
private String lastName ;
private PropertyChangeSupport pcs ;
public Person() {
pcs = = new PropertyChangeSupport(this);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcs.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcs.removePropertyChangeListener(listener);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
String oldName = this.firstName ;
this.firstName = firstName;
pcs.firePropertyChange("firstName", oldName, this.firstName);
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
String oldName = this.lastName ;
this.lastName = lastName;
pcs.firePropertyChange("lastName", oldName, this.lastName);
}
}
Now changes to the bean will propagate to the JavaFX property used by the table, as well as vice-versa.