JFileChooser returns incorrect path in OS X (folders only mode)

Posted by Virat Kadaru on Stack Overflow See other posts from Stack Overflow or by Virat Kadaru
Published on 2010-06-17T16:59:26Z Indexed on 2010/06/17 17:03 UTC
Read the original article Hit count: 159

Filed under:
|
|
|

I have a problem in java swing where the user has to select a folder, so I am using the code below.

JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

if(fc.showDialog(singleton, SELECT) == JFileChooser.APPROVE_OPTION) {
  File folder = fc.getSelectedFile();
  String path = folder.getPath() + File.separatorChar + MYAPPFOLDER;
}

Now there are 2 ways a user may select the folder

  1. Navigate to the folder and select the folder
  2. Navigate to the folder, go into the folder, and click select

Both ways work fine on windows but on OS X, I get

If I do 1 : path = Users/<username>/Desktop/MYAPPFOLDER

If I do 2 : path = Users/<username>/Desktop/Desktop/MYAPPFOLDER

How do I avoid this 2nd case?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about java

Related posts about swing