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

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

谢谢;-)

最佳答案

问题是您首先显示文件选择器对话框,然后仅设置其当前目录。

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

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/

相关文章:

java - 在运行时检查 Java 字节码是否包含调试符号

java - JFolderChooser.showOpenDialog 中的父组件是什么

java - 从 Play Framework 中的热重载中排除类

java - 在 JBDS/JBoss 工具中使用 Hibernate

Java - Swing - 在一个 JFrame 中显示多个 JComponent

java - 输入ENTER键不捕获

强制文件已经存在的java文件选择器

java - JFileChooser 不显示应用过滤器的文件

java - 大堆的 G1GC 日志舍入值

java - 如何从组合框中禁用/删除上下文菜单?