treeview构建2棵有关联的文件(夹)树,通过tornadofx提供的populate,仅用3行代码实现一棵树。并可在右侧窗口查看文本文件的内容和图片
代码语言:txt复制import com.dlsc.gemsfx.PDFView
import javafx.scene.control.*
import javafx.scene.image.Image
import javafx.scene.image.ImageView
import javafx.scene.layout.Priority
import javafx.scene.layout.StackPane
import javafx.scene.web.WebView
import org.apache.tika.Tika
import tornadofx.*
import java.io.File
class FileBrowserView : View("tornadofx入门23_treeview_advanced") {
val leftFile = objectProperty<File>()
val rightFile = objectProperty<File>()
val fileTyle = stringProperty()
lateinit var webEngine: WebView
val pdfview = PDFView().apply { vgrow = Priority.ALWAYS }
val img = objectProperty<Image>()
val txt = stringProperty()
lateinit var contentView: StackPane
// lateinit var txtV: TextArea
lateinit var imgV: ImageView
val rightRoot = objectProperty<TreeItem<File>>(TreeItem(File(".")))
lateinit var rightTree: TreeView<File>
val tika = Tika()
override val root = borderpane {
top {
}
left = hbox(4) {
treeview<File> {
root = TreeItem(File("."))
populate {
it.value.listFiles()?.asList()?.filter {
it.isDirectory
}
}
bindSelected(leftFile)
leftFile.onChange {
rightRoot.value = TreeItem(leftFile.value)
rightTree.populate {
it.value.listFiles()?.asList()
}
}
cellFormat {
text = it.name
graphic = nodeGraphic(it)
}
vgrow = Priority.ALWAYS
}
treeview<File> {
rightTree = this
// root = TreeItem(File("."))
rootProperty().bind(rightRoot)
isShowRoot = false
populate {
it.value.listFiles()?.asList()
}
bindSelected(rightFile)
rightFile.onChange {
val f = rightFile.value
displayFile(f)
}
cellFormat {
text = it.name
graphic =nodeGraphic(it)
}
vgrow = Priority.ALWAYS
}
}
center = vbox {
stackpane {
imageview(img) {
imgV = this
fitWidth = 400.0
fitHeight = 400.0
}
// textarea(txt) {
// txtV = this
// vgrow = Priority.ALWAYS
// }
webview {//用于显示文本内容
webEngine = this
vgrow = Priority.ALWAYS
}
stackpane {
label()
contentView = this
vgrow = Priority.ALWAYS
}
vgrow = Priority.ALWAYS
}
label(stringBinding(fileTyle) { "文件类型:${this.value}" })
}
setPrefSize(1024.0, 768.0)
}