Reputation: 302
I'm working in JavaFX with bindings and properties.
I have a Label label
and a Person currentPerson
.
I have the following code:
label.textProperty().bind(currentPerson.nameProperty());
Then I have in another section of code:
currentPerson = newPerson; //newPerson is a given Person instance
This way the textProperty
of label
doesn't update!
But if I do in that section of code:
currentPerson.setName(newPerson.getName());
then this updates the textProperty
of label
.
My question is: why does the second way update the textProperty
of label
, while the first doesn't, even though the nameProperty
of currentPerson
is changed in both cases?
Upvotes: 0
Views: 3277
Reputation: 3141
As mentioned, You've lost your first binding after :
currentPerson = newPerson;
The solution is either (re)bind currentPerson
after any assignment to currentPerson
, or instead, use a method to pass the newPerson
data, like:
currentPerson.setPerson(newPerson);
public class Person{
private StringProperty name = new SimpleStringProperty();
// ....
public void setPerson(Person person) {
// ....
this.name.set(person.name.get());
}
}
Upvotes: 2
Reputation: 585
I think the most basic answer to your question is that, after the line currentPerson = newPerson;
, the currentPerson
object is not the same object that was bound to label
previously.
Upvotes: 1
Reputation: 99
It must be occurring that you have set the bind
in relation to that person's name
, so when you use getName
, it updates the label
Upvotes: -1