scalafxml
scalafxml copied to clipboard
How to access the stage in a controller class?
With the annotation @sfxml
, the controller class is separated from the stage
variable. It seems that because of this separation, the stage
variable becomes not accessible (since it is outside of the controller class).
This is often needed, as shown in the following example:
1 @sfxml
2 class MyController(val btnTest: Button) {
3 btnTest.onMouseClicked = { e: MouseEvent =>
4 val fileChooser = new FileChooser()
5 fileChooser.showSaveDialog(/*stage should be here*/)
6 }
7 }
Line 5 requires access to stage
. Is there a way to do this in a controller annotated by @sfxml
?
Currently there is no direct way to do this, but you can create a helper class holding a reference to the stage, and pass it as a dependency to your controller.