0

I am trying to use the JFileChooser to have the user select a file. The absolute directory then needs to be in a string variable so I can use it in later code. So far, I have not found any solution/tips anywhere else online. So far I have...

FileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.getCurrentDirectory();
chooser.setDialogTitle("Select Image to detect facial presence");
chooser.setFileFilter(filter);

int returnVal = chooser.showOpenDialog(null);
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
DevilRunner
  • 51
  • 1
  • 7
  • 1
    Possible duplicate of [Get the path of a directory using JFileChooser](https://stackoverflow.com/questions/8444508/get-the-path-of-a-directory-using-jfilechooser) – Robert Moskal Oct 13 '17 at 17:46
  • Sir(@RobertMoskal) OP is using both the important things as mentioned in the above link. Isn't it? –  Oct 13 '17 at 17:49

2 Answers2

0

Based on Get the path of a directory using JFileChooser as indicated by @RobertMoskal's comment:

FileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.getCurrentDirectory();
chooser.setDialogTitle("Select Image to detect facial presence");
chooser.setFileFilter(filter);

int returnVal = chooser.showOpenDialog(null);

//ADDITIONAL CODE
String filePath;
if(returnVal == JFileChooser.APPROVE_OPTION) {
    filePath = chooser.getSelectedFile().getAbsolutePath()); //<<<<<
}
CosmicGiant
  • 6,275
  • 5
  • 43
  • 58
0
 JFileChooser chooser = new JFileChooser();
      FileNameExtensionFilter filter = new FileNameExtensionFilter(
          "JPG & GIF Images", "jpg", "gif");
      chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
      chooser.getCurrentDirectory();
      chooser.setDialogTitle("Select Image to detect facial presence");
      chooser.setFileFilter(filter);
      String temp;
      int returnVal = chooser.showOpenDialog(null);
      temp = chooser.getSelectedFile().getAbsolutePath();
DevilRunner
  • 51
  • 1
  • 7
  • You forgot to check for `APPROVE_OPTION`, as the chooser's dialog may also be cancelled or error-out. Other than that, our answers are pretty much identical (I also fixed the indentation and gave proper credits, but meh). – CosmicGiant Oct 13 '17 at 17:58