如何在动态列表视图上安装点击处理程序(在 tornadofx 中)

How do install a click handler on a dynamic listview (in 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) {
}

要在 selected ListView 中的项目时配置回调,请使用 onUserSelect 回调:

onUserSelect {
    information("You selected $it")
}

您也可以选择传递构成 select 的点击次数,默认值为 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)