gpt4 book ai didi

Javafx 链接/绑定(bind) Treeview 项目到 ObservableList

转载 作者:行者123 更新时间:2023-11-30 10:12:37 25 4
gpt4 key购买 nike

我正在尝试找到一种简单的方法将 Download 类型的 TreeView 链接到相同类型的 ObservableList

主 Controller .java

public class MainController {

private ObservableList<Download> downloads = FXCollections.observableArrayList();
@FXML private TreeView<Download> $TreeDownloads;

@FXML
public void initialize() {
$TreeDownloads.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
$TreeDownloads.setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT);
$TreeDownloads.setShowRoot(false);

downloads.addListener(new ListChangeListener<Download>() {
@Override
public void onChanged(Change<? extends Download> c) {
if (c.wasAdded()) {
addDownloads(c.getAddedSubList());
}
if (c.wasRemoved()) {
//
}
}
});
downloads.add(new Download("3847"));
downloads.add(new Download("3567"));
downloads.add(new Download("2357"));
}

private void addDownloads(List<? extends Download> downloads) {
downloads.forEach(download -> {
TreeItem<Download> treeItem = new TreeItem<>(download);
$TreeDownloads.getRoot().getChildren().add(treeItem);
new Thread(download::start).start();
});
}

private void removeDownloads(List<? extends Download> downloads) {
// remove treeitems from the treeview that hold these downloads
}
}

下载.java

public class Download {

private DoubleProperty progress = new SimpleDoubleProperty(0D);
private StringProperty id = new SimpleStringProperty("");

public Download(String id) {
this.id.set(id);
}

public void start() {
while (progress.getValue() < 1) {
try {
Thread.sleep(1000);
progress.add(0.1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@Override
public String toString() {
return id.getValue();
}
}

我如何通过Object(Download) 机制实现remove,是否有更简单的方法将observablelist 的项目绑定(bind)到 TreeView ?

最佳答案

仍然不完全确定确切的问题是什么,一切都很简单:

首先,您的列表更改监听器实现不正确,它必须在访问其状态之前推进 subChanges(您确实运行了发布的代码;)

downloads.addListener(new ListChangeListener<Download>() {
@Override
public void onChanged(Change<? extends Download> c) {
// this while was missing
while (c.next()) {
if (c.wasAdded()) {
addDownloads(c.getAddedSubList());
}
if (c.wasRemoved()) {
// accessing the list of removed elements is .. plain standard api
removeDownloads(c.getRemoved());
}

}
}
});

现在实现相应treeItems的移除:

private void removeDownloads(List<? extends Download> downloads) {
// remove treeitems from the treeview that hold these downloads
List<TreeItem<Download>> treeItemsToRemove = treeDownloads.getRoot().getChildren().stream()
.filter(treeItem -> downloads.contains(treeItem.getValue()))
.collect(Collectors.toList());
treeDownloads.getRoot().getChildren().removeAll(treeItemsToRemove);
}

旁白:

  • java 命名约定对成员使用小写字母:treeDownloads(不是 $TreeDownloads)
  • MCVE 中的“可验证”意味着可以按原样运行:张贴者应该是第一个验证这一点的人;)你的不是由于监听器的错误实现
  • MCVE 中的“最小”意味着去掉所有不需要的东西:f.i.调用线程代码——这在您的第一个片段中特别令人分心,因为违反 fx 的线程规则是一个相当常见的错误

关于Javafx 链接/绑定(bind) Treeview 项目到 ObservableList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51787549/

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