TornadoFX 模态对话框不会自动调整高度

Posted

技术标签:

【中文标题】TornadoFX 模态对话框不会自动调整高度【英文标题】:TornadoFX modal dialog isn't automatically adjusting height 【发布时间】:2020-10-20 18:26:12 【问题描述】:

我有一个 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://***.com/a/28559958/519035
            managedProperty().bind(visibleProperty())
        

        button("OK")
    

控制器打开它就像

inputView.openModal(owner = primaryStage)

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

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

【问题讨论】:

【参考方案1】:

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

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://***.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")
    

【讨论】:

以上是关于TornadoFX 模态对话框不会自动调整高度的主要内容,如果未能解决你的问题,请参考以下文章

单个模态的离子3模态全屏宽度/高度?

引导模态高度上的转换不起作用

模态对话框不会随 stopPropagation 关闭

Vue模态框的封装

Bootstrap模态对话框怎么隐藏?

模态与非模态对话框