1

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?

mr nooby noob
  • 1,860
  • 5
  • 33
  • 56
  • 1
    possible duplicate of [JFileChooser change default directory in Windows](http://stackoverflow.com/questions/13516829/jfilechooser-change-default-directory-in-windows) – Madhawa Priyashantha Sep 22 '15 at 18:51

3 Answers3

2

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");
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
  • cool that works! thanks! Is there another way to read that file without giving the full path name? The reason why I ask this is because if I where to give this project to someone and have it run on their computer, the path for them would not be the same. How can I get it to read from a folder, lets say named "My Pics", if it is saved in my java project? – mr nooby noob Sep 22 '15 at 19:00
2

You can either add directory to the constructor of JFileChooser like this:

JFileChooser fileChooser = new JFileChooser("directory");

or you can set the current directory using setCurrentDirectory(File dir):

fileChooser.setCurrentDirectory(new File("directory"));

It is probably easier to just set it with the constructor, but if you need to change it after creating the JFileChooser, use setCurrentDirectory(File dir).

Ladas125
  • 265
  • 3
  • 14
0

You can either add directory to the constructor of JFileChooser like this:

 JFileChooser fileChooser = new JFileChooser();
    fileChooser.setCurrentDirectory(new File("put here your directory"));
    int result = fileChooser.showOpenDialog(getParent());
    if (result == JFileChooser.APPROVE_OPTION) 
    {
        File selectedFile = fileChooser.getSelectedFile();
        jTextField.setText(selectedFile.getAbsolutePath());
    }