我试图让 JFileChooser 记住上次打开的位置,然后下次打开那里,但似乎不记得了。我必须打开它两次: 第一次运行时效果很好。但在第二次运行时,第一次运行时的路径仍然被锁定。我必须打开 JFileChooser 对话框两次才能获取更新的路径...
//Integrate ActionListener as anonymous class
this.openItem.addActionListener(new java.awt.event.ActionListener() {
//Initialise actionPerformed
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
//Generate choose file
this.chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = this.chooser.showOpenDialog(PDFcheck.this.openItem);
if (this.theOutString != null){
this.chooser.setCurrentDirectory(new File(this.theOutString)); }
if(returnVal == JFileChooser.APPROVE_OPTION) {
//theOutString = fc.getSelectedFile().getName();
this.theOutString = this.chooser.getSelectedFile().getPath();
System.out.println("You chose to open this file: " + this.theOutString);}
}
private String theOutString;
private final JFileChooser chooser = new JFileChooser();
});
谢谢;-)
最佳答案
问题是您首先显示文件选择器对话框,然后仅设置其当前目录。
您应该首先设置当前目录,然后显示对话框:
if (this.theOutString != null)
this.chooser.setCurrentDirectory(new File(this.theOutString));
int returnVal = this.chooser.showOpenDialog(PDFcheck.this.openItem);
关于java - 如何让 JFileChooser 记住以前的文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25422489/