我刚开始使用TornadoFX,我发现文档很少涉及如何使用ObservableMap
来填充TreeView
并在基础数据更改时动态地重新呈现。
我的应用程序允许用户将文件拖放到目标区域。它们被添加到一个可观察的列表中,我在该列表中创建了一个ObjectBinding<Map<File,List<File>>>
并按父目录对其进行分组。我想将TreeView中的Map反映为目录和文件的父子关系。
如果为files
赋予了初始值,但以下内容将起作用,但不会更新树以反映对其的更改:
class DrawableView : View("Drawables") {
private var filesList: TreeView<File> by singleAssign()
private val files = FXCollections.observableArrayList<File>()
private val folders = Bindings.createObjectBinding(Callable {
files.groupBy { File(it.parent) }
},files)
override val root = vbox {
filesList = treeview {
root = TreeItem(File("All"))
isShowRoot = false
cellFormat { text = it.name }
populate { node ->
val item = node.value
when {
node == root -> folders.value.keys
item is File -> folders.value[item]
else -> null
}
}
}
}
...
}
有什么建议吗?这是预期的结果: