Javafx链接/绑定Treeview项到ObservableList

时间:2018-08-10 13:22:23

标签: java javafx javafx-8 javafx-2

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

MainController.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
    }
}

Download.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)机制实现删除,是否有更简单的方法将可观察列表的项目绑定到树视图?

1 个答案:

答案 0 :(得分:1)

仍不能完全确定确切的问题是什么,一切都非常简单:

首先,您的列表更改侦听器实现不正确,必须先访问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的线程规则是一个相当常见的错误