我創建了一個JFileChooser來打開一個文件,但是當我選擇一個文件並打開它時,第二次我想選擇一個文件時,JFileChooser不在當前目錄中。 如何設置JFileChooser打開當前目錄?設置JFileChooser打開當前目錄
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION){
File fileName = fileChooser.getSelectedFile();
File path=fileChooser.getCurrentDirectory();
if ((fileName == null) || (fileName.getName().equals("")))
{
JOptionPane.showMessageDialog(this, "Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE);
}
else{
currentPath=path.getPath()+"\\"+fileName.getName();}
}