我如何将两个子串绑定到一个?

时间:2016-07-17 12:28:30

标签: java data-binding javafx

我有两个字符串

private StringProperties firstName;
private StringProperties lastName;
private StringProperties nickName;

用户选择名字和姓氏,nickName是第一个和最后一个名字的前3个字符的串联

我怎么能这样做?

实际上我就像那样初始化它(这是整个班级)。

public class Person {
private StringProperty firstName;
private StringProperty lastName;
private StringProperty nickName;
private ObservableList<Evento> eventi = FXCollections.observableArrayList();

public Person(String firstName, String lastName) {
    this.firstName = new SimpleStringProperty(firstName);
    this.lastName = new SimpleStringProperty(lastName);
    if (firstName.length() > 2 && lastName.length() > 2)
        this.nickName = new SimpleStringProperty(firstName.trim().substring(0,3).concat(lastName.trim().substring(0,3)));
    else
        this.nickName = new SimpleStringProperty("");
}

public ObservableList<Evento> getEventi() {
    return eventi;
}

public String getFirstName() {
    if(firstName == null) firstName = new SimpleStringProperty(this,"firstName");
    return firstName.get();
}

public StringProperty firstNameProperty() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName.set(firstName);
}

public String getLastName() {
    if(lastName == null) lastName = new SimpleStringProperty(this, "lastName");
    return lastName.get();
}

public StringProperty lastNameProperty() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName.set(lastName);
}

public String getNickName() {
    if(nickName == null) nickName = new SimpleStringProperty(this,"nickName");
    return nickName.get();
}

public StringProperty nickNameProperty() {
    return nickName;
}

@Override
public String toString() {
    return getNickName() + "(" + getLastName() + " " + getFirstName() + ")";
}

}

但是当我让用户更改first或lastName时,nickName不会更新。

2 个答案:

答案 0 :(得分:5)

您应该使用ReadOnlyStringProperty作为昵称:

private ReadOnlyStringWrapper nickName= new ReadOnlyStringWrapper();
...
public final String getNickName() {
    return nickName.get();
}

public final ReadOnlyStringProperty nickNameProperty() {
    return nickName.getReadOnlyProperty();
}

对于绑定,您可以使用Bindings类中的实用程序方法,或者为任何其他复杂情况实现自己的绑定。此示例使用createStringBinding()方法。需要Callable函数接口,它将用于计算新值,以及可观察属性列表,将观察更改的值:

public Person(String firstName, String lastName) {
    this.firstName = new SimpleStringProperty(firstName);
    this.lastName = new SimpleStringProperty(lastName);

    this.nickName.bind(Bindings.createStringBinding(()->{
        if(this.firstName.get().length() > 2 && this.lastName.get().length() > 2) {
            return this.firstName.get().substring(0,3).concat(this.lastName.get().trim().substring(0,3));
        } else {
            return "";
        }
    }, this.firstName, this.lastName));

}

答案 1 :(得分:4)

您可以使用Bindings.format

nickName.bind(Bindings.format("%.3s%.3s", firstName, lastName));

3中的%.3s是字符串的最大长度。

虽然不会对字符串进行任何修剪,但是在将字符串传递给firstNamelastName之前,您可以这样做。)

它也适用于小于3个字符的字符串。因此,您可以获得FoBarFooBBar等昵称(如果名字是空字符串)。