Possible Duplicat开发者_运维百科e: How do I restrict JFileChooser to a directory?
JFileChooser FileC = new JFileChooser("C:\messy");
int result = FileC.showOpenDialog(this);
if( result == JFileChooser.CANCEL_OPTION )
{
return;
}
I have it starting from the folder C:\messy
, but currently a user can go to all directories from this starting position.
Single Root File Chooser limits the slection to a single directory and its children.
If you want to prevent the selection of child directories then you would also need to add a FileFilter:
chooser.removeChoosableFileFilter( chooser.getAcceptAllFileFilter() );
chooser.addChoosableFileFilter( new FileFilter()
{
public boolean accept(File f)
{
return ! f.isDirectory();
}
public String getDescription()
{
return "Files only";
}
});
精彩评论