如何让JFileChooser记住以前的文件夹?

时间:2014-08-21 09:14:14

标签: java swing jfilechooser

我试图让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();
         });

谢谢; - )

1 个答案:

答案 0 :(得分:2)

问题是你首先显示文件选择器对话框,然后你只设置它的当前目录。

首先应首先设置当前目录,然后显示对话框:

if (this.theOutString != null)
    this.chooser.setCurrentDirectory(new File(this.theOutString));
int returnVal = this.chooser.showOpenDialog(PDFcheck.this.openItem);