sách gpt4 ăn đã đi

java - 装饰 ObservableList 并保留更改事件的最佳实践

In lại 作者:搜寻专家 更新时间:2023-11-01 02:42:03 28 4
mua khóa gpt4 giày nike

我的数据源提供了一个 ObservableList ,但是对于我的 ListView,我需要一个 ObservableList .

MỘT Warning基本上只是字符串的装饰器,添加一个 boolean 值以提供一种方法来跟踪我的 ListView 的复选框状态,如 cái này 中所建议的那样回答。

class Warning {

private final ReadOnlyStringWrapper name;
private final BooleanProperty checked;

/* ... */

}

目前我正在观察原始列表中的更改事件并手动添加/删除警告列表中的项目:

ObservableList stringList = ...;
ObservableList warningList = ...;

stringList.addListener(new ListChangeListener() {

@Ghi đè
public void onChanged(ListChangeListener.Change change) {
if (change.wasAdded()) {
warningList.addAll(change.getAddedSubList().stream().map(Warning::new).collect(Collectors.toList()));
} else if (change.wasRemoved()) {
change.getRemoved().forEach(str -> {
warningList.removeIf(w -> str.equals(w.name));
});
}
}

});

Câu hỏi của tôi là:是否有更优雅的方式来修饰我的字符串类型列表,以便它可以用作警告类型列表而无需手动传递更改事件?

更准确地说:如果将字符串添加到原始列表或从原始列表中删除,我希望立即在警告列表和 ListView 中看到此更改。

câu trả lời hay nhất

自从您发布后,我一直在思考这个问题。按照我在评论中建议的那样使用 EasyBind 是行不通的,因为每次您在映射列表上调用 get(...) 时它都会创建一个新的 Warning . Vì thế

stringList.add("foo");
warningList.get(0).setChecked(true);
assert warningList.get(0).isChecked();

会失败。

此外,如果您在源列表 (stringList) 中有重复的条目,您的机制就会出错(我认为),因为您会从 warningList 中删除所有相应的条目当从 stringList 中删除单个元素时。事实上,正确获取删除的元素非常棘手。

这是一个基于 Tomas Mikula 的 MappedList 的解决方案它缓存源元素和映射元素之间的映射。它使用 IdentityHashMap 来确保重复元素在两个列表中的行为正确。请注意,这仅适用于您希望在将项目添加到源列表时创建新对象的特定情况,因此它不打算(也不会工作)替代 EasyBind 中的机制。

nhập java.util.ArrayList;
import java.util.IdentityHashMap;
nhập java.util.List;
import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.collections.transformation.TransformationList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.CheckBoxListCell;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class WrappedObjectListExample extends Application {

@Ghi đè
public void start(Stage primaryStage) {
ObservableList stringList = FXCollections.observableArrayList("One", "Two", "Three");
ObservableList warningList = new CachingMappedList(stringList, Warning::new);

ListView stringListView = new ListView<>(stringList);
ListView warningListView = new ListView<>(warningList);

warningListView.setCellFactory(CheckBoxListCell.forListView(Warning::checkedProperty));

TextField textField = new TextField();
textField.setOnAction(e -> {
if (! textField.getText().isEmpty()) {
stringList.add(textField.getText());
textField.setText("");
}
});

Button remove = new Button("Remove");
remove.setOnAction(e -> stringList.remove(stringListView.getSelectionModel().getSelectedIndex()));
remove.disableProperty().bind(stringListView.getSelectionModel().selectedItemProperty().isNull());

HBox lists = new HBox(10, stringListView, warningListView);
VBox root = new VBox(10, lists, textField, remove);
root.setPadding(new Insets(20));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}

public static class Warning {
private final ReadOnlyStringWrapper name = new ReadOnlyStringWrapper();
private final BooleanProperty checked = new SimpleBooleanProperty();

public Warning(String name) {
this.name.set(name);
}

public final ReadOnlyStringProperty nameProperty() {
return this.name.getReadOnlyProperty();
}

public final String getName() {
return this.nameProperty().get();
}

public final BooleanProperty checkedProperty() {
return this.checked;
}

public final boolean isChecked() {
return this.checkedProperty().get();
}

public final void setChecked(final boolean checked) {
this.checkedProperty().set(checked);
}

@Ghi đè
công khai String toString() {
return getName();
}

}

public static class CachingMappedList extends TransformationList {

private final Function mapper ;

private final IdentityHashMap cache ;

public CachingMappedList(ObservableList source, Function mapper) {
super(source);
this.mapper = mapper ;
this.cache = new IdentityHashMap<>();
}

@Ghi đè
protected void sourceChanged(Change c) {

fireChange(new Change(this) {

@Ghi đè
public boolean wasAdded() {
return c.wasAdded();
}

@Ghi đè
public boolean wasRemoved() {
return c.wasRemoved();
}

@Ghi đè
public boolean wasReplaced() {
return c.wasReplaced();
}

@Ghi đè
public boolean wasUpdated() {
return c.wasUpdated();
}

@Ghi đè
public boolean wasPermutated() {
return c.wasPermutated();
}


@Ghi đè
public boolean next() {
return c.next();
}

@Ghi đè
công khai void reset() {
c.reset();
}

@Ghi đè
public int getFrom() {
return c.getFrom();
}

@Ghi đè
public int getTo() {
return c.getTo();
}

@Ghi đè
public List getRemoved() {
List removed = new ArrayList<>();
c.getRemoved().forEach(t -> removed.add(cache.get(t)));
return removed;
}

@Ghi đè
public int getPermutation(int i) {
return c.getPermutation(i);
}

@Ghi đè
protected int[] getPermutation() {
throw new AssertionError("Unreachable code");
}

});

// clean up cache:

c.reset();
while (c.next()) {
if (c.wasRemoved()) {
c.getRemoved().forEach(cache::remove);
}
}
}

@Ghi đè
public int getSourceIndex(int index) {
return index ;
}

@Ghi đè
public S get(int index) {
return cache.computeIfAbsent(getSource().get(index), mapper);
}

@Ghi đè
public int size() {
return getSource().size();
}
}

public static void main(String[] args) {
khởi chạy(đối số);
}
}

关于java - 装饰 ObservableList 并保留更改事件的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31230312/

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress