簡體   English   中英

如何使JFileChooser記住先前的文件夾?

[英]how to get a JFileChooser to remember a previous folder?

我試圖讓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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM