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?
FileChooser class is a part of JavaFX. It is used to invoke file open dialogs for selecting a single file (showOpenDialog), file open dialogs for selecting multiple files (showOpenMultipleDialog) and file save dialogs (showSaveDialog). FileChooser class inherits Object class.
APPROVE_OPTION. Return value if approve (yes, ok) is chosen. static String. APPROVE_SELECTION. Instruction to approve the current selection (same as pressing yes or ok).
JFileChooser fc = new JFileChooser(); int returnVal = fc. showSaveDialog(frame); if (returnVal == JFileChooser. APPROVE_OPTION){ File file = fc. getSelectedFile(); if (file.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With