Reputation: 733
I have created a TableView using the the code below;
TableView<Person> tableView = new TableView<>();
TableColumn<Person,String> firstNameCol = new TableColumn<>("First Name");
firstNameCol .setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
tableView.getColumns().add(firstNameCol);
tableView.getItems().add(new Person("John"));
firstNameCol.setCellFactory(TextFieldTableCell.<Person>forTableColumn()));
and my Model is the following;
class Person{
private SimpleStringProperty firstName;
public Person(String firstName){
this.firstName = new SimpleStringProperty(firstName);
}
public final void setFirstName(String value){
firstName.set(value);
System.out.println("first name updated");
}
public final String getFirstName(){
return firstName.get();
}
public SimpleStringProperty firstNameProperty(){
return firstName;
}
}
Now, when I edit the First Name column I should get the "first name updated" output but I am not, which means the set method of the model's property doesn't get called. Shouldn't this be the case or my understanding is wrong? Thanks in advance.
Upvotes: 0
Views: 41
Reputation: 82491
You return the property from the cellValueFactory
. Assuming you do not specify a onEditCommit
event handler for the the column, the SimpleStringProperty
object is modified. The setter method of the class containing the field is not involved.
You should be able to verify this by adding a listener to the property in the constructor or by extending SimpleStringProperty
:
this.firstName = new SimpleStringProperty(firstName) {
@Override
public void set(String value) {
super.set(value);
System.out.println("first name updated (SimpleStringProperty.set)");
}
};
or
this.firstName.addListener((o, oldValue, newValue) -> System.out.println("first name updated (SimpleStringProperty.set)"));
Upvotes: 2