Search code examples
javakotlinjavafxtornadofx

TornadoFX modal dialog isn't automatically adjusting height


I have a TornadoFX application that opens a modal dialog. It has text field and can show error if input is invalid.

The problem is that the modal dialog doesn't automatically adjust its height when the error is shown or hidden.

This is how the simplified dialog looks like without error and with error

enter image description here enter image description here

And here's a simplified view:

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")
    }
}

Controller opens it like

inputView.openModal(owner = primaryStage)

TornadoFX and JavaFX have lots of configurations, like prefHeight, usePrefHeight, fitToHeight, maxHeightProperty, vgrow. I've played around with them, but so far no luck.

Could someone please point to the correct way of making this dialog to automatically adjust its height?


Solution

  • With the help of another question I was able to find a solution. I had to add following to the node with error:

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

    The final simplified code looks like

    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")
        }
    }