可以使用相对StringProperty创建动态ObservableList吗?
例如,使用下面的代码,如何动态地重新创建它并在必要时添加新的StringProperty?
private final ObservableList<Record> recordList = FXCollections.observableArrayList();
public static class Record {
private static int trackId;
private final SimpleIntegerProperty id;
private final SimpleStringProperty name;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private Record(String name, String lastName, String email) {
this.id = new SimpleIntegerProperty(trackId);
this.name = new SimpleStringProperty(name);
this.lastName = new SimpleStringProperty(lastName);
this.email = new SimpleStringProperty(email);
trackId++;
}
public int getId() {
return this.id.get();
}
public void setId(int id) {
this.id.set(id);
}
public String getName() {
return this.name.get();
}
public void setName(String name) {
this.name.set(name);
}
public String getLastName() {
return this.lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public String getEmail() {
return this.email.get();
}
public void setEmail(String email) {
this.email.set(email);
}
}发布于 2014-02-14 20:12:11
即使使用反射,也不能将字段添加到类中。也许改变架构会更好?让我们看看下一个类:
class Property <T> {
private final T propertyValue;
private final String propertyName;
public Property (String name, T value) {
this.propertyName = name;
this.propertyValue = value;
}
public T getValue(){
return propertyValue;
}
public String getName(){
return propertyName;
}
}这个类帮助你创建新的属性并存储它。现在,您可以创建属性列表并将其存储在类记录中。现在,您可以动态添加新属性。在我看来,它更灵活。
https://stackoverflow.com/questions/21775385
复制相似问题