开发者

How to do JFilechooser to allow the user only to select from one folder only? [duplicate]

开发者 https://www.devze.com 2023-03-10 05:04 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicat开发者_运维百科e: How do I restrict JFileChooser to a directory?
This question already has answers here: Closed 11 years ago.

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";
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消