scalafxml icon indicating copy to clipboard operation
scalafxml copied to clipboard

How to access the stage in a controller class?

Open jyuhuan opened this issue 9 years ago • 1 comments

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?

jyuhuan avatar Feb 11 '16 16:02 jyuhuan

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.

vigoo avatar Feb 18 '16 15:02 vigoo