如何在动态列表视图上安装点击处理程序(在tornadofx中)
我的应用程序需要允许添加到列表视图。 我想通过使用observableArrayList
动态添加到列表视图。 如果我点击按钮,项目被添加到列表并显示。
现在我正在努力添加一个点击处理程序(我想处理当有人点击列表视图中的任何项目时发生的事件)。 我在哪里做这个?
这是我的代码。
package someapp import javafx.collections.FXCollections import javafx.geometry.Pos import javafx.scene.layout.VBox import javafx.scene.text.FontWeight import tornadofx.* class MyApp : App(HelloWorld::class) { } class HelloWorld : View() { val leftSide: LeftSide by inject() override val root = borderpane { left = leftSide.root } } class LeftSide: View() { var requestView: RequestView by singleAssign() override val root = VBox() init { with(root) { requestView = RequestView() this += requestView this += button("Add Item") { action { requestView.responses.add( Request( "example.com", "/foo/bar", "{ \"foo\" : \"bar\"}".toByteArray())) } } } } } class RequestView : View() { val responses = FXCollections.observableArrayList<Request>( ) override val root = listview(responses) { cellFormat { graphic = cache { form { fieldset { label(it.hostname) { alignment = Pos.CENTER_RIGHT style { fontSize = 22.px fontWeight = FontWeight.BOLD } } field("Path") { label(it.path) } } } } } } } class Request(val hostname: String, val path: String, val body: ByteArray) { }
要在选择ListView中的项目时配置回调,请使用onUserSelect
回调:
onUserSelect { information("You selected $it") }
您可以选择传递多少点击构成一个选择,默认值是2:
onUserSelect(1) { information("You selected $it") }
你在你的代码中使用了一些过时的结构,这里是一个更新的版本转换为最佳实践:)
class MyApp : App(HelloWorld::class) class HelloWorld : View() { override val root = borderpane { left(LeftSide::class) } } class LeftSide : View() { val requestView: RequestView by inject() override val root = vbox { add(requestView) button("Add Item").action { requestView.responses.add(Request("example.com", "/foo/bar", """{ "foo" : "bar"}""".toByteArray())) } } } class RequestView : View() { val responses = FXCollections.observableArrayList<Request>() override val root = listview(responses) { cellFormat { graphic = cache { form { fieldset { label(it.hostname) { alignment = Pos.CENTER_RIGHT style { fontSize = 22.px fontWeight = FontWeight.BOLD } } field("Path") { label(it.path) } } } } } onUserSelect(1) { information("You selected $it") } } } class Request(val hostname: String, val path: String, val body: ByteArray)
- 从Java迁移到Kotlin
- 如何防止对话(警报)在您使用Anko触摸外部或后退时关闭
- Android Studio上的Kotlin(非Android)项目的ClassNotFoundException错误
- TextColor正在改变到错误的一个
- 将Thread / Runnable实现从Java转换到Kotlin
- 为什么我不能说:val list = properties.map((key,value) – >“$ key = $ value”)
- Kotlin – 如何做onCompleteListener从Firestore获取数据?
- 如何通过键加入两个RxJava2 Obvervables?
- 如何使用Kotlin定向JavaScript的依赖注入?