6

I am looking solution for javafx FileChooser(in Kotlin). I stuck on this, I cannot pass root View, because Window! is expected:

button("open some file") {

                    setOnAction {
                        val fileChooser = FileChooser();
                        val file = fileChooser.showOpenDialog(???)
                        ...
                    }

                }

What should I pass in this case?

Herohtar
  • 5,347
  • 4
  • 31
  • 41
Lukas
  • 1,216
  • 12
  • 25
  • 4
    Although Ruckus gave you the correct answer below (use the `chooseFile` and `chooseDirectory` functions in TornadoFX), I just wanted to point out that you can access the stage via the `primaryStage` property. If your View is opened using `openModal` or `openWindow` you can access your stage via the `modalStage` property of the `View`. `Stage` inherits from `Window`. – Edvin Syse Nov 22 '16 at 08:19

2 Answers2

9

According to the docs you can pass a null for the window.

If the owner window for the file dialog is set, input to all windows in the dialog's owner chain is blocked while the file dialog is being shown.

However, since you are using TornadoFX, you may instead just want to use the chooseFile and chooseDirectory functions it provides. They automatically handle the hairy parts for you with useful defaults, but (since they are only defaults after all) you can easily override them to tailor the functionality to your needs.

Ruckus T-Boom
  • 4,566
  • 1
  • 28
  • 42
4

The following code worked for me:

with(root) {
    button("Target Directory") {
        action {
            var dir = chooseDirectory("Select Target Directory")
        }
    }
}

On Windows, the file chooser dialogue will open "My Computer" by default.