gpt4 book ai didi

项目字段更改时 JavaFX 更新 FilteredList

转载 作者:行者123 更新时间:2023-11-29 03:06:18 25 4
gpt4 key购买 nike

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

最佳答案

使用 JavaFX properties pattern 创建模型类(已附加) .

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

关于项目字段更改时 JavaFX 更新 FilteredList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32037526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com