什么应该传递给 FileChooser?

What should be passed to FileChooser?

我正在寻找 javafx FileChooser(在 Kotlin 中)的解决方案。我坚持这一点,我无法通过根视图,因为预计 Window!:

button("open some file") {

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

                }

在这种情况下我应该传递什么?

According to the docs 你可以为 window.

传递一个 null

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.

但是,由于您使用的是 TornadoFX,您可能只想使用它提供的 chooseFilechooseDirectory 函数。它们使用有用的默认值自动为您处理毛茸茸的部分,但是(因为它们毕竟只是默认值)您可以轻松地覆盖它们以根据您的需要定制功能。

以下代码对我有用:

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

在 Windows 上,文件选择器对话框将默认打开 "My Computer"。