0

I'm trying to get the JFileChooser to remember the location of the previous location opened, and then next time open there, but is doesn't seem to remember. I have to open it twice: At the first run it works fine. But at the second run there's still the path locked from the first run. I have to open the JFileChooser dialog twice to get the newer path...

//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();
         });

thanks ;-)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). – Andrew Thompson Aug 21 '14 at 09:56
  • I'm sorry. Next time I'll do it! –  Aug 21 '14 at 13:36
  • Does this answer your question? [Is there any way to make Java file selection dialogs remember the last directory?](https://stackoverflow.com/questions/8282048/is-there-any-way-to-make-java-file-selection-dialogs-remember-the-last-directory) – Mahozad Sep 27 '22 at 19:09

1 Answers1

2

Problem is that you first show the file chooser dialog, and you only set its current directory after that.

You should first set the current directory first and then show the dialog:

if (this.theOutString != null)
    this.chooser.setCurrentDirectory(new File(this.theOutString));
int returnVal = this.chooser.showOpenDialog(PDFcheck.this.openItem);
icza
  • 389,944
  • 63
  • 907
  • 827
  • oh my godness!!! It works perfect :-) thank you so much. I completely overlooked it. I'm a beginner and thankful for each help. –  Aug 21 '14 at 10:26