制作一个对话框,用户可以在其中选择文件或文件夹
Making a dialog where the user can choose either a file or a folder
在 MATLAB 中,有一个函数可以提示用户选择一个或多个文件 - uigetfile
, and there's another function which allows the user to choose a folder - uigetdir
。
我想为用户提供选择文件或文件夹的能力,使用单个window,因为这对我要创建的用户体验很重要。
到目前为止,我发现使用上述功能的唯一解决方案1需要一个额外的步骤来询问用户他们想要什么类型的实体select,提前,并相应地调用适当的函数——我觉得这很不方便。
那么我怎样才能有一个允许我 select 任一个的对话框?
我们可以为此使用 Java 组件,特别是 JFileChooser
,并确保我们为其提供 FILES_AND_DIRECTORIES
选择标志。
%% 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
这会生成一个看起来很熟悉的对话框,如下所示,它的工作完全符合预期:
JFileChooser
有各种有趣的设置,例如 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。
在 R2018a 上测试,Java 1.8。0_144(java.lang.System.getProperty("java.version")
的输出)。
在 MATLAB 中,有一个函数可以提示用户选择一个或多个文件 - uigetfile
, and there's another function which allows the user to choose a folder - uigetdir
。
我想为用户提供选择文件或文件夹的能力,使用单个window,因为这对我要创建的用户体验很重要。
到目前为止,我发现使用上述功能的唯一解决方案1需要一个额外的步骤来询问用户他们想要什么类型的实体select,提前,并相应地调用适当的函数——我觉得这很不方便。
那么我怎样才能有一个允许我 select 任一个的对话框?
我们可以为此使用 Java 组件,特别是 JFileChooser
,并确保我们为其提供 FILES_AND_DIRECTORIES
选择标志。
%% 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
这会生成一个看起来很熟悉的对话框,如下所示,它的工作完全符合预期:
JFileChooser
有各种有趣的设置,例如 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。
在 R2018a 上测试,Java 1.8。0_144(java.lang.System.getProperty("java.version")
的输出)。