按 JFileChooser 中的“保存”按钮将打开一个文件夹而不是保存文件
我在使用 JFileChooser 时遇到以下问题:我创建了一个保存对话框,然后当我尝试保存文件时,我在字段中输入文件名并单击“保存”。假设我尝试保存文件的目录包含一个子目录,其名称与输入的名称完全相同。在这种情况下,JFileChooser 打开它而不是保存我的文件,并且我不知道如何让它保存文件而不是打开同名的目录。
我尝试使用文件过滤器进行一些操作,使用 approveSelection()
并将 UI 从 Windows 更改为默认值 - 没有任何帮助。如何使 JFileChooser 停止打开文件夹并保存文件?有什么方法可以修改按钮的行为还是我只是做错了什么?
更新: 需要明确的是:我并不是试图保存具有相同名称的文件,我只是想让 JFileChooser 获取路径,以便我可以向其附加扩展名。然而,我检查了Word和Photoshop,它们都无法满足我的要求,所以我想我希望一切都变得太完美。但如果有办法做我想做的事,我仍然会很高兴听到解决方案。
I'm having the following problem with a JFileChooser: I create a save dialog and then when I'm trying to save a file, I'm entering a file name into the field and click "Save". Suppose that the directory, which I try to save my file to, contains a subdirectory, which is named exactly like the entered name. In this case JFileChooser opens it instead of saving my file, and I don't know how to make it save a file instead of opening the directory with the same name.
I tried to do something with the file filters, with approveSelection()
and changed UI from Windows to default - nothing helps. How to make JFileChooser stop opening a folder and save the file? Is there some way to modify the behavior of the button or am I just doing something wrong?
Update:
To be clear: I'm not trying to save a file with the same name, I'm just trying to make JFileChooser get the path so I could append an extension to it. However, I checked Word and Photoshop, neither of them is able to do what I request, so I guess I wish to make everything too perfect. But if there's a way to do what I want, I'll still be glad to hear the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,Windows和Linux都不允许同一目录中的文件名和目录名相同。当然在 Linux 下它们可以是相同的但大小写不同:)
Neither Windows nor Linux allow file names and directory names to be the same in the same directory as far as I know. Under Linux of course they can be the same with different case lettering :)