0

是否可以创建具有相对 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);
    }
}
4

1 回答 1

0

即使将使用反射,您也不能将字段添加到类中。也许改变架构更好?让我们看看下一节课:

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;
    }
}

此类可帮助您创建新属性并存储它。现在,您可以创建属性列表并将其存储在类 Record 中。现在您可以动态添加新属性。我认为它更灵活。

于 2014-02-14T12:12:11.237 回答