Tal*_*n06 9 java swing jfilechooser
我在应用程序中使用JFileChooser来浏览目录,但是当我选择目录时,它返回我选择的文件夹上方文件夹的路径.即我选择"C:\ Test"并返回"C:\"
这是我正在使用的代码
JFileChooser c = new JFileChooser();
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int rVal = c.showSaveDialog(c);
if (rVal == JFileChooser.APPROVE_OPTION) {
txtDirectory.setText("");
CC_Test.MsgBox(c.getCurrentDirectory().toString());
txtDirectory.setText(c.getCurrentDirectory().toString());
}
if (rVal == JFileChooser.CANCEL_OPTION) {
txtDirectory.setText("");
}
Run Code Online (Sandbox Code Playgroud)
How*_*ard 15
你应该用
c.getSelectedFile()
Run Code Online (Sandbox Code Playgroud)
代替
c.getCurrentDirectory()
Run Code Online (Sandbox Code Playgroud)
为了获得所选文件(在这种情况下也称为目录).否则它会生成filechooser面板(父级)中显示的目录,而不是选中的目录.