Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a dialog where the user can choose either a file or a folder

In MATLAB, there's a function that prompts the user to choose one or more files - uigetfile, and there's another function which allows the user to choose a folder - uigetdir.

I would like to provide the user an ability to choose either a file or a folder, using a single window, as this is important for the UX I'm trying to create.

So far, the only solution I found that uses the above functions1 requires an additional step of asking the user what type of entity they would like to select, in advance, and calling the appropriate function accordingly - which I find inconvenient.

So how can I have a dialog which allows me to select either one?

like image 439
Dev-iL Avatar asked Jul 20 '18 10:07

Dev-iL


People also ask

How to select files in matlab?

file = uigetfile opens a modal dialog box that lists files in the current folder. It enables a user to select or enter the name of a file. If the file exists and is valid, uigetfile returns the file name when the user clicks Open. If the user clicks Cancel or the window close button (X), uigetfile returns 0 .

How do I use file chooser?

Click the Open a File button. Navigate around the file chooser, choose a file, and click the dialog's Open button. Use the Save a File button to bring up a save dialog. Try to use all of the controls on the file chooser.

What does Uigetfile () return if the user selects cancel?

If the user clicks the Cancel button or closes the dialog window, FileName and PathName are set to 0 . [FileName,PathName,FilterIndex] = uigetfile(...) returns the index of the filter selected in the dialog box.


1 Answers

We can use a Java component for this, specifically JFileChooser, and make sure that we provide it with the FILES_AND_DIRECTORIES selection flag.

%% Select entity:
jFC = javax.swing.JFileChooser(pwd);
jFC.setFileSelectionMode(jFC.FILES_AND_DIRECTORIES);
returnVal = jFC.showOpenDialog([]);
switch returnVal
  case jFC.APPROVE_OPTION
    fName = string(jFC.getSelectedFile());
  case jFC.CANCEL_OPTION
    % do something with cancel
  case jFC.ERROR_OPTION
    % do something with error
  otherwise
    throw(MException("fileFolderChooser:unsupportedResult", ...
                     "Unsupported result returned from JFileChooser: " + returnVal + ...
                     ". Please consult the documentation of the current Java version (" + ...
                     string(java.lang.System.getProperty("java.version")) + ")."));
end

%% Process selection:
switch true % < this is just some trick to avoid if/elseif
  case isfolder(fName)
    % Do something with folder
  case isfile(fName)
    % Do something with file
  otherwise
    throw(MException('fileFolderChooser:invalidSelection',...
                     'Invalid selection, cannot proceed!'));
end

This produces a familiar-looking dialog as follows, which works exactly as expected:

Selection dialog

JFileChooser has a variety of interesting settings like multi-selection and showing hidden files/folders, as well as standard settings like changing the dialog title, the button texts and tooltips, etc. It can also be used as either an "Open" dialog or a "Save" dialog simply by setting a value.

Tested on R2018a, with Java 1.8.0_144 (output of java.lang.System.getProperty("java.version")).

like image 195
Dev-iL Avatar answered Sep 22 '22 23:09

Dev-iL