方便在kotlin确认javafx对话框

时间:2016-02-02 11:04:50

标签: javafx kotlin

我试图使用JavaFX编写自己的确认对话框,当按下 okButton 时会调用lambda表达式。

ConfirmDialog.show("Delete selected items?") {
    // actions...
}

此实现中的问题是永远不会调用来自actionByConfirm函数的show()。请告诉我,如何纠正它?

object ConfirmDialog : UnborderedStage() {
    override val pathToScene: String
        get() = "/module/main/confirm/confirm.fxml"


    val title: Label

    val okButton: Button
    val cancelButton: Button

    var actionByConfirm: (() -> Unit)? = null

    init {
        title = scene.lookup("#title") as Label
        okButton = scene.lookup("#buttonOk") as Button
        cancelButton = scene.lookup("#buttonCancel") as Button

        okButton.setOnMouseClicked {
            actionByConfirm?.invoke()
            hide()
        }

        cancelButton.setOnMouseClicked { hide() }
    }

    fun show(title: String, actionByConfirm: () -> Unit) {
        this.title.text = title
        this.actionByConfirm = actionByConfirm
        show()
    }
}

* UnborderedStage是同一个舞台,但是通过聚焦 StageStyle.UNDECORATED 关闭动作。

0 个答案:

没有答案