0
JFileChooser filechooser = new JFileChooser();
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnValue = chooser.showOpenDialog(this);
if(returnValue == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this directory: " +
filechooser.getSelectedFile().getAbsolutePath());

this is the code i used to open a file and get its path printed but the thing is i want to get the path of a exe file which means path should end up with the file extension at the end. with the current code it won't even show the exe files.

2 Answers2

2

If you need exe files, you can use a Filter, but with the good options, like that (your code is Directories oriented ):

JFrame frame=new JFrame();
JFileChooser filechooser = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("EXE File","exe");
filechooser.setFileFilter(filter);
filechooser.showOpenDialog(frame);
File file = filechooser.getSelectedFile();
System.out.println("YOU CHOOSE "+file.getAbsolutePath());

a usefull link on that question: FileFilter for JFileChooser

see this for the option filechooser.setFileSelectionMode:

JFileChooser select directory but show files

Community
  • 1
  • 1
0

Of course it won't show files, you're using

filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Andre
  • 778
  • 1
  • 5
  • 23