【问题标题】:JavaFX updating FilteredList when item field has changed项目字段更改时JavaFX更新FilteredList
【发布时间】:2015-08-17 03:53:16
【问题描述】:

我有 ObservableList 和 FilteredList,其中包含带有字段 state 的附件。 FilteredList 设置为 ListView。我想在将字段state更改为State.REMOVED时,已更新FilteredList。

/* class Item */
private final ObservableList<Attached> attaches = FXCollections.observableArrayList();
private final FilteredList<Attached> filteredAttaches = attaches.filtered(attached -> attached.getState() != Attached.State.REMOVED);

/* Controller */
listAttached.setItems(item.getAttachesForDisplay());

/* class Attached */
public class Attached {

public static enum State {
    NEW, ATTACHED, REMOVED
}

private State state;
private final String path;
private final String name;

public Attached(State state, String path, String name) {
    this.state = state;
    this.path = path;
    this.name = name;
}

public State getState() {
    return state;
}

public void changeState(State state) {
    this.state = state;
    // Generate some event for update filtered list? 
}

public String getPath() {
    return path;
}

public String getName() {
    return name;
}

@Override
public String toString() {
    return name;
}

}

【问题讨论】:

    标签: java javafx javafx-8


    【解决方案1】:

    使用JavaFX properties pattern 创建您的模型类 (Attached)。

    public class Attached {
    
        public static enum State {
            NEW, ATTACHED, REMOVED
        }
    
        private final ObjectProperty<State> state = new SimpleObjectProperty<>();
        private final StringProperty path = new SimpleStringProperty();
        private final StringProperty name = new SimpleStringProperty();
    
        public Attached(State state, String path, String name) {
            setState(state);
            setPath(path);
            setName(name);
        }
    
        public ObjectProperty<State> stateProperty() {
            return state ;
        }
    
        public final State getState() {
            return stateProperty().get();
        }
    
        public final void setState(State state) {
           stateProperty().set(state);
        }
    
        public StringProperty pathProperty() {
            return path ;
        }
    
        public final String getPath() {
            return pathProperty.get();
        }
    
        public final void setPath(String path) {
            pathProperty().set(path);
        }
    
        public StringProperty nameProperty() {
            return name ;
        }
    
        public final String getName() {
            return nameProperty().get();
        }
    
        public final void setName(String name) {
            nameProperty().set(name);
        }
    
        @Override
        public String toString() {
            return getName();
        }
    
    }
    

    现在使用extractor 创建基础列表,以便在stateProperty 更改时触发更新事件:

    private final ObservableList<Attached> attaches = 
        FXCollections.observableArrayList(attached -> new Observable[]{attached.stateProperty()});
    private final FilteredList<Attached> filteredAttaches = 
        attaches.filtered(attached -> attached.getState() != Attached.State.REMOVED);
    

    【讨论】:

      猜你喜欢
      • 2015-04-10
      • 2017-07-22
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多