Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should be passed to FileChooser?

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?

like image 698
Lukas Avatar asked Nov 21 '16 23:11

Lukas


People also ask

What is a FileChooser?

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.

What is approve_ option?

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).

How do I find my JFileChooser file name?

JFileChooser fc = new JFileChooser(); int returnVal = fc. showSaveDialog(frame); if (returnVal == JFileChooser. APPROVE_OPTION){ File file = fc. getSelectedFile(); if (file.


2 Answers

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.

like image 137
Ruckus T-Boom Avatar answered Sep 24 '22 02:09

Ruckus T-Boom


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.

like image 37
Keith Skronek Avatar answered Sep 24 '22 02:09

Keith Skronek