Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use java.awt.FileDialog to only allow the user to select folders

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 ?)

like image 314
Paul Taylor Avatar asked Dec 03 '25 17:12

Paul Taylor


1 Answers

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)

like image 130
MadProgrammer Avatar answered Dec 06 '25 08:12

MadProgrammer