【发布时间】:2017-02-23 16:34:53
【问题描述】:
我只是对TornadoFX 和Kotlin 中的cellCache 函数用法感到困惑。渲染总是奇怪和出乎意料的,会发生什么?代码在这里:
data class Person(var name : String, var age : Int, var sex : Boolean)
class MyView: View()
{
val list = listOf<Person>(
Person("Name A", 20, false),
Person("Name B", 22, false),
Person("Name C", 21, true),
Person("Name D", 30, true),
Person("Name E", 35, true)
).observable()
override val root = stackpane {
vbox {
tableview(items = list) {
column("Name", Person::name)
column("Age", Person::age)
column("Sex", Person::sex).cellCache{
checkbox{
isSelected = it
setOnAction {
println("Selection: $isSelected")
}
}
}
}
}
}
}
我希望所有行都会在"Sex" 列中呈现checkbox,但上面的代码只会显示两个:一个selected 和另一个unselected,cellcache 到底是怎么回事?
我还发现把data class改成下面的,就可以了:
data class Person(var name : String, var age : Int, var sex : SimpleBooleanProperty)
但是我必须以这种方式对数据类属性进行很多更改,我也不知道为什么。 帮助我,在此先感谢!
【问题讨论】:
-
对不起,Person 类的最后一个代码不应该使用数据类,而是使用普通类前缀。这有效:
class Person(var name : String, var age : Int, sex : Boolean) { var sex by property(SimpleBooleanProperty(sex)) fun sexProperty() = getProperty(Person::sex) }
标签: javafx tableview kotlin tornadofx