JFileChooser选择目录但显示文件 [英] JFileChooser select directory but show files

查看:176
本文介绍了JFileChooser选择目录但显示文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得应该有一个简单的方法来做到这一点,但我无法弄明白。我有一个允许用户选择目录的JFileChooser。我想显示目录中的所有文件以向用户提供一些上下文,但是只应接受目录作为选择(可能在选择文件时禁用打开按钮)。有没有一种简单的方法可以做到这一点?

I feel like there should be a simple way to do this but I can't figure it out. I have a JFileChooser that allows the user to select directories. I want to show all the files in the directories to give the user some context, but only directories should be accepted as selections (maybe the Open button would be disabled when a file is selected). Is there an easy way of doing this?

推荐答案

覆盖approveSelection()方法。类似于:

Override the approveSelection() method. Something like:

JFileChooser chooser = new JFileChooser( new File(".") )
{
    public void approveSelection()
    {
        if (getSelectedFile().isFile())
        {
            // beep
            return;
        }
        else
            super.approveSelection();
    }
};

这篇关于JFileChooser选择目录但显示文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆