如何使用 TornadoFX treeview 显示数据

How to display data using TornadoFX treeview

我正在学习如何使用 kotlin 并已开始使用 tornadoFX。我正在阅读指南以尝试学习它,但是我无法弄清楚 'TreeView with Differing Types' 中的含义。好像说我应该使用星投影,当你在调用中使用 * 时我就知道了。

然而,一旦我这样做,树视图就会显示 'Projections are not allowed on type arguments of functions and properties'

这是我的代码:

class 主视图:视图(“”){

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,我将不胜感激:)

看来指南实际上是不正确的。我使用 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
        }
    }
}

当这个 post 挽救了我的一天时,我以为我想完全退出 tornadofx。在我的例子中,我想显示一个对象的嵌套列表。我没想到需要像 else -> null 这样的东西来防止 stackoverlow。不知何故,我最终得到了这个现在对我有用的填充块

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