Sheikh
Sheikh

Reputation: 539

fileChooser.setDialogType() Doesn't Work

I have code that is supposed to choose a folder from a JFileChooser, but it still is acting like its choosing a file, even if I use fs.setDialogType. I tried showSaveDialog and showOpenDialog, but they both don't work. Here is my code:

public static String getFolder() {
    JFileChooser fs = new JFileChooser();
    fs.setDialogType(JFileChooser.DIRECTORIES_ONLY);
    fs.showSaveDialog(null);
    if (fs.getSelectedFile() != null)
        return fs.getSelectedFile().getAbsolutePath();
    return "null";
}

Upvotes: 0

Views: 283

Answers (1)

Reimeus
Reimeus

Reputation: 159864

setDialogType is used to set the open, save or custom type for the dialog. Use setFileSelectionMode to specify the whether the dialog should select files or directories

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 

Upvotes: 2

Related Questions