The following example shows the declaration of a property (StringProperty
in this case) and demonstrates how to add a ChangeListener
to it.
import java.text.MessageFormat;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
public class Person {
private final StringProperty name = new SimpleStringProperty();
public final String getName() {
return this.name.get();
}
public final void setName(String value) {
this.name.set(value);
}
public final StringProperty nameProperty() {
return this.name;
}
public static void main(String[] args) {
Person person = new Person();
person.nameProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println(MessageFormat.format("The name changed from \"{0}\" to \"{1}\"", oldValue, newValue));
}
});
person.setName("Anakin Skywalker");
person.setName("Darth Vader");
}
}