Search code examples
javajfilechooser

How to use JFileChooser.showOpenDialog to open a specific file?


Right now I am able to open a up any file that I want, however the default file that opens up is My Documents. How can I set the default path to a file that is saved in my java project?

Right now this is what I have:

              try{
                  int option = chooser.showOpenDialog(MyPanel.this);//Chooser is my JFileChooser
                    if(option == JFileChooser.APPROVE_OPTION) {
                       //do stuff
                    }
              }catch(Exception ex){} 

What do I have to pass into showOptionDialog() to open a folder if it is located in my java project?


Solution

  • You can use like

    JFileChooser chooser = new JFileChooser("desired_current_directory");
    

    or

    chooser.setCurrentDirectory(new File("desired_current_directory"));
    

    If you want to open My Pics folder under your project directory use

    JFileChooser chooser = new JFileChooser("./My Pics");