amira
amira

Reputation: 434

How to set minHeight and minWidth to a Window in TornadoFX?

I am using TornadoFX in my project. I have a root View which is a borderPane. I was able to find setPrefSize(1200.0, 720.0) and it works perfectly. However, by default the window is resizable, which user can resize it without any limits. Is there any property or method to set minHeight and minWidth of a window so that when resized, it stops at those limits?

So far I have tried these but none of them seem to work:

override val root = borderPane {
   minHeight = 400.0
   minWidth = 600.0
   setMinSize(600.0, 400,0)
   setWindowMinSize(600, 400) // this throws NPE
   addClass(loginScreen) // I even set properties in CSS
}


// StyleClass

loginScreen{
   minWidth = 600.px
   minHeight = 400.px
}

What is the proper way of setting minHeight and minWidth of a window in TornadoFX? And one more thing, how do I actually disable window resizing in TornadoFX, there is no property called isResizable? P.S. I am a super novice in Kotlin and in TornadoFX. Just started it today.

Upvotes: 4

Views: 1884

Answers (1)

Edvin Syse
Edvin Syse

Reputation: 7297

You are on the right track. The preferred size of the root component will become the initial size of the window. You can further configure a minimum size for the window, but you can't do it in the constructor of the UIComponent, since it is created before the actual window it will be shown in. For that reason, the onDock callback is a good place to configure the window with setWindowMinSize. If this is the main application window though, an even better strategy would be to override start and configure the minWidth and minHeigh properties for the Stage (which is a Window).

It is worth noting that the convenience function setWindowMinSize is only available if you override onDock in your UIComponent, since it's defined as a shortcut to set the min size of the currentStage for the UIComponent. If you override start, you have to manipulate the properties directly, like this:

class MyApp : App(MainView::class) {
    override fun start(stage: Stage) {
        with(stage) {
            minWidth = 600.0
            minHeight = 400.0
            super.start(this)
        }
    }
}

class MainView : View() {
    override val root = borderpane {
        setPrefSize(1200.0, 720.0)
    }
}

You can set isResizable = false on the Stage/Window in the start function as well if you want to prevent resizing.

Upvotes: 4

Related Questions