Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"))
   }
}
like image 574
mojtab23 Avatar asked Jul 23 '16 15:07

mojtab23


People also ask

How do you set a scene in a stage?

You can add a Scene object to the stage using the method setScene() of the class named Stage.

How would you set the title of the stage primary stage?

setTitle("New Title!"); O primaryStage = "New Title!; O primary Stage.

What method is invoked to display a stage in JavaFX?

A stage is displayed by invoking the show() method on the stage. 14.4 What is the output of the following JavaFX program?


2 Answers

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.

like image 189
Edvin Syse Avatar answered Oct 13 '22 15:10

Edvin Syse


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.

like image 29
Ruckus T-Boom Avatar answered Oct 13 '22 15:10

Ruckus T-Boom