3

I'm trying to use FileDialog instead of JFileChooser to get more natural behaviour on OSX, especially important is the Favourites column with clear links to shared folders that are hidden under /Volumes using JFileChooser.

I'm using Java 7, and for that reason I'm not using Quaqua JFileChooser as it hasnt been updated for a year and Im unsure if it compatible with Oracles Java 7.

But Im hitting a problem, is there a way to get FileDialog to only allow a folder to be opened rather than a file, I set a filename filter but it seemed to have no effect and there is no

.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

like there is for JFileChooser.

public void actionPerformed(ActionEvent e)
{
    FileDialog chooser = new FileDialog(SongKong.mainWindow.frame);
    chooser.setFilenameFilter(new FolderFilter());
    chooser.setMode(FileDialog.LOAD);
    chooser.setVisible(true);
    String folderSelected = chooser.getDirectory();
    File folder = new File(folderSelected) ;
    if(folder.exists() && folder.isDirectory())
    {
        //Do something with selected folder
    }
}

class FolderFilter implements FilenameFilter
{
    public boolean accept(File dir, String name)
    {
        return new File(dir,name).isDirectory();
    }
}

(As an aside tried the code on WIndows 7 as well but still looks like Windopws XP dialog even though meant to be a native dialog, how come ?)

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • 3
    You could have a look at http://stackoverflow.com/questions/1224714/how-can-i-make-a-java-filedialog-accept-directories-as-its-filetype-in-os-x – MadProgrammer Aug 23 '12 at 21:11
  • @PaulTaylor Did you try using setting the L&F with [`UIManager`](http://docs.oracle.com/javase/7/docs/api/javax/swing/UIManager.html) to see how `JFileChooser`s looked on OS X? That might be another option if you haven't – Jeffrey Aug 23 '12 at 21:14
  • Yes Im using the System Look and Feel, colours are all Maclike but dialog that opens just provide access to userfolder, my home folder and harddrive but doesnt provide any of the favourites stuff – Paul Taylor Aug 24 '12 at 09:38

1 Answers1

1

As to your first question, check the link in my comments

As to the second, I would suggest that it comes down to which libraries they are linking to in order to facility the functionality. Just because the OS has updated doesn't mean that the old libraries have been removed. In order to maintain compatibility with older versions of applications, these libraries are normally maintained for some time.

You could take a look at xFileDialog (via this post Alternative to JFileChooser)

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Thanks that fixes the folder problem, however I'm disappointed that on OSX the native open dialog provided doesn't appear much different to the JFileChooser , and nowhere near as good as Quaqua File Chooser. Unfortunately xFileDialog is not useful as the main problem is with the application on OSX rather than Windows. – Paul Taylor Aug 24 '12 at 09:41
  • I've marked your answer as correct as you've answered both my questions but unfortunately i havent solved my undrlying problem of not having an OSX like file dialog yet. – Paul Taylor Aug 24 '12 at 10:23
  • Oops I messed up source control, actually native dialog looks great on OSX, unfortunately the apple.awt.fileDialogForDirectories option is broken , but due for fixing in final version of 1.7.0_08 i.e anytime soon. – Paul Taylor Aug 24 '12 at 12:06
  • I'm kinda curious about this know, I've not tried it, but you might like to take a look @ http://java.dzone.com/news/native-dialogs-swing-little – MadProgrammer Aug 24 '12 at 12:27
  • thanks, but Im happy with the Native OSX dialog, just waiting for the bug fix. Anything not developed specifically for OSX is not going to work very well. – Paul Taylor Aug 24 '12 at 12:33