4

我正在学习如何使用 kotlin 并开始使用 tornadoFX。我正在阅读该指南以尝试学习它,但是我无法弄清楚“具有不同类型的 TreeView”中的含义。似乎说我应该使用星形投影,正如我在通话中使用 * 时所知道的那样。

但是,一旦我这样做,树视图就会说“不允许对函数和属性的类型参数进行投影”

这是我的代码:

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

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

4

2 回答 2

7

看来指南实际上是不正确的。我让它工作了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
        }
    }
}
于 2017-09-26T15:37:47.497 回答
0

当这篇文章拯救了我的一天时,我想我想完全退出 tornadofx。就我而言,我想显示对象的嵌套列表。我没想到else -> null需要类似的东西来防止stackoverlow。不知何故,我最终得到了这个现在对我有用的填充块

populate { parent -> val value = parent.value 
when
{
    parent == root -> quotation.houses
    value is NewHouse -> value.rooms
    else -> null
}}
于 2019-07-17T07:21:14.520 回答