首页 > 解决方案 > TornadoFX 模态对话框不会自动调整高度

问题描述

我有一个 TornadoFX 应用程序,它打开一个模式对话框。它有文本字段,如果输入无效,会显示错误。

问题是模式对话框在显示或隐藏错误时不会自动调整其高度。

这就是简化的对话框看起来没有错误和错误的样子

在此处输入图像描述 在此处输入图像描述

这是一个简化的视图:

class InputView : View("Enter text") {
    private val textProperty = SimpleStringProperty()

    override val root: VBox = vbox {
        label("Enter text below:")

        textfield(textProperty)

        label("Error message") {
            visibleWhen(textProperty.isNotEmpty)
            // This is necessary so that hidden error is really removed, see https://stackoverflow.com/a/28559958/519035
            managedProperty().bind(visibleProperty())
        }

        button("OK")
    }
}

控制器打开它就像

inputView.openModal(owner = primaryStage)

TornadoFX 和 JavaFX 有很多配置,例如 prefHeight、usePrefHeight、fitToHeight、maxHeightProperty、vgrow。我和他们一起玩过,但到目前为止还没有运气。

有人可以指出使此对话框自动调整其高度的正确方法吗?

标签: javakotlinjavafxtornadofx

解决方案


在另一个问题的帮助下,我找到了解决方案。我不得不在节点中添加以下错误:

visibleProperty().onChange {
    currentWindow?.sizeToScene()
}

最终的简化代码看起来像

class InputView : View("Enter text") {
    private val textProperty = SimpleStringProperty()

    override val root: VBox = vbox {
        label("Enter text below:")

        textfield(textProperty)

        label("Error message") {
            visibleWhen(textProperty.isNotEmpty)

            // This is necessary so that hidden error is really removed, see https://stackoverflow.com/a/28559958/519035
            managedProperty().bind(visibleProperty())
            
            // This is necessary to automatically adjust dialog height when error is shown or hidden
            visibleProperty().onChange {
                currentWindow?.sizeToScene()
            }
        }

        button("OK")
    }
}

推荐阅读