如何使用TornadoFX treeview显示数据

时间:2017-09-26 12:23:54

标签: treeview kotlin tornadofx

我正在学习如何使用kotlin并开始使用tornadoFX。我正在浏览指南以尝试学习它,但是我无法弄清楚具有不同类型的树视图中的含义是什么。似乎我应该使用星形投影,正如我在电话中使用*时所知道的那样。

然而,只要我这样做,树视图就会说'不允许对函数和属性的类型参数进行预测。

这是我的代码:

类MainView:查看(""){

override val root = treeview<*> {
        root = TreeItem(Person("Departments", ""))

        cellFormat {
            text = when (it) {
                is String -> it
                is Department -> it.name
                is Person -> it.name
                else -> throw IllegalArgumentException("Invalid Data Type")
            }
        }

        populate { parent ->
            val value = parent.value
            if (parent == root) departments
            else if (value is Department) persons.filter { it.department == value.name }
            else null
        } }

}

我老老实实地难倒,我不知道自己应该做些什么。

另外,如果其他人可以为我提供一些学习Kotlin和tornadoFX的有用链接,我们将不胜感激:)

2 个答案:

答案 0 :(得分:2)

看来指南实际上是不正确的。我使用treeview<Any>

开始工作
data class Department(val name: String)
data class Person(val name: String, val department: String)

val persons = listOf(
        Person("Mary Hanes", "Marketing"),
        Person("Steve Folley", "Customer Service"),
        Person("John Ramsy", "IT Help Desk"),
        Person("Erlick Foyes", "Customer Service"),
        Person("Erin James", "Marketing"),
        Person("Jacob Mays", "IT Help Desk"),
        Person("Larry Cable", "Customer Service")
)

val departments = persons.groupBy { Department(it.department) }

override val root = treeview<Any> {
    root = TreeItem("Departments")
    cellFormat {
        text = when (it) {
            is String -> it
            is Department -> it.name
            is Person -> it.name
            else -> kotlin.error("Invalid value type")
        }
    }
    populate { parent ->
        val value = parent.value
        when {
            parent == root -> departments.keys
            value is Department -> departments[value]
            else -> null
        }
    }
}

答案 1 :(得分:0)

当这篇文章挽救了我的生活时,我以为我想完全放弃tornadofx。就我而言,我想显示一个对象的嵌套列表。我没想到像else -> null这样的东西可以防止stackoverlow。不知何故,我最终得到了这个填充块,该填充块现在对我有用

populate { parent -> val value = parent.value 
when
{
    parent == root -> quotation.houses
    value is NewHouse -> value.rooms
    else -> null
}}