2014-02-18 30 views
1

我不想指定一個目錄。我只想讓它自動「知道」並在用戶所在的目錄中打開。我該怎麼做?如何讓JFileChooser在用戶所在的當前目錄中打開?

+2

你不知道。你需要告訴它。當'currentDirectory'爲'null'時,它將在構造時使用'FileSystemView#getDefaultDirectory'。坦率地說,這是很多工作。您可以爲每個基本任務創建一個「JFileChooser」實例(一個用於保存,一個用於打開例如),並簡單地維護該實例,該實例將「記住」它最後使用的目錄,您仍然需要通過 – MadProgrammer

回答

2

此示例在首次顯示時默認爲user.dir。然後,它按照MadProgrammer的建議,保留選擇器的實例以自動跟蹤上次加載或保存位置。

enter image description here

enter image description here

enter image description here

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 
import java.io.*; 

public class ChooserInCurrentDir { 

    // the GUI as seen by the user (without frame) 
    JPanel gui = new JPanel(new BorderLayout()); 
    JFileChooser fileChooser; 
    private JTextArea output = new JTextArea(10, 40); 

    ChooserInCurrentDir() { 
     initComponents(); 
    } 

    public final void initComponents() { 
     gui.setBorder(new EmptyBorder(2, 3, 2, 3)); 

     String userDirLocation = System.getProperty("user.dir"); 
     File userDir = new File(userDirLocation); 
     // default to user directory 
     fileChooser = new JFileChooser(userDir); 

     Action open = new AbstractAction("Open") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       int result = fileChooser.showOpenDialog(gui); 
       if (result == JFileChooser.APPROVE_OPTION) { 
        try { 
         File f = fileChooser.getSelectedFile(); 
         FileReader fr = new FileReader(f); 
         output.read(fr, f); 
         fr.close(); 
        } catch (Exception ex) { 
         ex.printStackTrace(); 
        } 
       } 
      } 
     }; 

     Action save = new AbstractAction("Save") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       int result = fileChooser.showSaveDialog(gui); 
       throw new UnsupportedOperationException("Not supported yet."); 
      } 
     }; 

     JToolBar tb = new JToolBar(); 
     gui.add(tb, BorderLayout.PAGE_START); 
     tb.add(open); 
     tb.add(save); 

     output.setWrapStyleWord(true); 
     output.setLineWrap(true); 

     gui.add(new JScrollPane(
       output, 
       JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
       JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)); 
    } 

    public final JComponent getGui() { 
     return gui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       ChooserInCurrentDir cicd = new ChooserInCurrentDir(); 
       JFrame f = new JFrame("Chooser In Current Dir"); 
       f.add(cicd.getGui()); 
       // Ensures JVM closes after frame(s) closed and 
       // all non-daemon threads are finished 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       // See http://stackoverflow.com/a/7143398/418556 for demo. 
       f.setLocationByPlatform(true); 

       // ensures the frame is the minimum size it needs to be 
       // in order display the components within it 
       f.pack(); 
       // should be done last, to avoid flickering, moving, 
       // resizing artifacts. 
       f.setVisible(true); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency 
     SwingUtilities.invokeLater(r); 
    } 
} 
0

如果要設置文件選擇器上一次打開時所處的目錄,則需要設置當前目錄。

//This will set the directory to the directory they previously chose a file from. 
fileChooser.setCurrentDirectory(fileChooser.getCurrentDirectory()); 

林不知道這是不是你要找的是什麼,但是,當他們選擇一個文件,然後去選擇其他文件,它會保持他們在從以前選擇的目錄。

+0

將種子與起始目錄結合起來'這會將目錄設置爲他們以前選擇文件的目錄.'因爲它不會改變任何內容,所以它毫無意義。我們希望得到的最好的結果是編譯器/ JVM意識到它毫無意義,並且完全跳過它。 –

2

基本上,你不能。你需要告訴它。

當使用nullcurrentDirectory(例如默認構造函數)構造時,它將使用FileSystemView#getDefaultDirectory

您可以爲每個基本任務創建一個JFileChooser的單個實例(一個用於保存,一個用於打開例如)並簡單地維護該實例,該實例將「記住」它最後使用的目錄,您仍然需要將其與起始目錄結合起來

另一種選擇是構建某種類型的庫調用,它可以根據某個唯一鍵加載並保存用戶使用的最後一個目錄。這意味着你可以簡單地做這樣的事情......

File toFile = MyAwesomeLibrary.getSaveFile(APPLICATION_DOCUMENT_SAVE_KEY); 

這將加載附帶的密鑰最後已知的目錄中,並顯示與該值配置的JFileChooser和將能夠返回如果選擇Filenull的用戶取消了操作...例如...

0

如果你希望你的應用程序來尋找用戶的工作目錄,你可以試試這個:

String curDir = System.getProperty("user.dir"); 
相關問題