JFileChooser change default directory in Windows

前端 未结 7 1016
粉色の甜心
粉色の甜心 2021-02-12 16:27

I want to change the default directory of my JFileChooser to \"My Music\" on Windows. This directory is C:\\Users\\Fre\\Music on my account because my username is <

相关标签:
7条回答
  • why don't you just give the FileChooser the path when you create it, like:

    JFileChooser chooser = new JFileChooser("C:\\Users\\Fre\\Music\\");
    
    0 讨论(0)
  • 2021-02-12 16:40

    Pretty Simple:

    JFileChooser browseImageFile = new JFileChooser("User Defined Directory");
    
    0 讨论(0)
  • 2021-02-12 16:53

    Creating all your own code, so as to set a default file directory is unnecessary and lengthy. A much easier and quicker way of doing it is by just right clicking on the File Chooser itself on Design view and right clicking 'customise code'.

    Customise Code for File Chooser

    This will show you the vital code for that GUI component. From the drop down box next to the top line of code, select 'custom creation'.

    This will allow you to customise what fileChooser = is assigned to. Between the curly brackets JFileChooser() you can either hard code in the file directory with speech marks like this.

    JFileChooser("C:\Users\user\Documents")
    

    or type in a name that for a variable you created earlier. This variable would hold the file directory. I would recommend the latter option, though either will work fine.

    Hope this helps.

    p.s. sorry about having to use a link for the photo. I don't have enough privilege yet.

    0 讨论(0)
  • 2021-02-12 16:54

    You can use the API method setCurrentDirectory when initializing your JFileChooser objects:

    public void setCurrentDirectory(File dir)
    

    Sample usage might be like:

    yourFileChooser.setCurrentDirectory(new File  
    (System.getProperty("user.home") + System.getProperty("file.separator")+ "Music"));
    
    0 讨论(0)
  • 2021-02-12 16:54

    Sorry for taking your time, Just found the answer myself:

    String userhome = System.getProperty("user.home");
    JFileChooser fc = new JFileChooser(userhome +"\\Music");
    
    0 讨论(0)
  • 2021-02-12 16:54
    JFileChooser openFile = new JFileChooser("C:\\Users\\Fre\\Music");
    
    0 讨论(0)
提交回复
热议问题