mojtab23
mojtab23

Reputation: 2675

Way of setting PrimaryStage or Scene properties in TornadoFX

I am new to tornadoFX and I don't know how to setup PrimaryStage or Scene properties like Scene height or width or PrimaryStage modality. Please help me.

UPDATE

I want to set Scene height and width, Look at this example:

dependencies {
compile 'no.tornado:tornadofx:1.5.2'
compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.3"
}


import javafx.scene.control.Label
import javafx.scene.layout.VBox
import tornadofx.App
import tornadofx.FX
import tornadofx.View

class Main : App() {
   override val primaryView = MyView::class

   init {
      // this two lines have error ( Val cannot be reassigned. )
      FX.primaryStage.scene.height = 600.0
      FX.primaryStage.scene.width = 800.0
      // or this line causes this exception ( java.lang.NoSuchMethodException )
      FX.primaryStage.isResizable = false
   }

}

class MyView : View() {
   override val root = VBox()

   init {
      root.children.add(Label("My label"))
   }
}

Upvotes: 11

Views: 5837

Answers (2)

Edvin Syse
Edvin Syse

Reputation: 7297

If you don't want to let the primary view dictate the initial scene size, you can override App.start and configure the dimensions of the primary stage, which again will dictate the dimensions of the scene:

override fun start(stage: Stage) {
    super.start(stage)
    stage.width = 800.0
    stage.height = 600.0
}

To make this even simpler, there will be a function in TornadoFX 1.5.3 that let you create the Scene for the primary view yourself:

override fun createPrimaryScene(view: UIComponent) = Scene(view.root, 800.0, 600.0)

The end result will be the same, so you can just keep the code in the first example though.

Upvotes: 24

Ruckus T-Boom
Ruckus T-Boom

Reputation: 4786

You should definitely check out the TornadoFX Guide. It's a great resource for getting started in TornadoFX.

To answer your question, you can set the size in the view's root. This should do what you want (usning TornadoFX's builder pattern):

class Main : App(MyView::class)

class MyView : View() {
    override val root = vbox {
        prefWidth = 800.0
        prefHeight = 600.0

        label("My label")
    }
}

Another option is to use type safe stylesheets:

class Main : App(MyView::class, Style::class)

class MyView : View() {
    override val root = vbox {
        label("My label")
    }
}

class Style : Stylesheet() {
    init {
        root {
            prefHeight = 600.px
            prefWidth = 800.px
        }
    }
}

The advantage of the type safe stylesheet is you can use different units (you could set just as easily say prefHeight = 10.cm or prefWidth = 5.inches). It can basically do anything CSS can do, but is much more convenient, powerful, and (as the name suggests) type safe.

Disclaimer: I was involved in designing and building the type safe stylesheet system for TornadoFX.

Upvotes: 10

Related Questions