使用 JFileChooser 并预先填充文件名保存?

发布于 2024-08-04 11:32:29 字数 441 浏览 3 评论 0原文

我试图让我制作的一些 GUI 的保存和加载变得更容易,并且我希望能够在保存时为用户预先填充文件名。

让 JFileChooser 指向一个方便的目录很容易,但预填充名称似乎并不那么容易。目前,我的代码是:

JFileChooser f = new JFileChooser();
f.setSelectedFile(new File(generateName()));

这实际上首先看起来有效:文件名填充在 JFileChooser 中,但是当单击“保存”按钮时,选择器只是将文件视图模式切换到要保存的文件名的模式(如果您不这样做)理解,你只需尝试一下就可以看到)。这可能是因为它指向的文件尚不存在。

如果用户更改文件名并尝试保存,它会起作用,但这破坏了整个要点。

我一直在寻找一种在字段中简单设置文本的方法,但它似乎没有任何直观的访问。有什么想法吗?

I am trying to make saving and loading easier for some GUIs that I've made, and I would like to be able to pre-populate a filename for the user on save.

Getting the JFileChooser to point at a convenient directory is easy enough, but pre-populating the name doesn't seem so easy. Currently, my code is:

JFileChooser f = new JFileChooser();
f.setSelectedFile(new File(generateName()));

This actually appears to work at first: The filename is populated in the JFileChooser, but when clicking the save button, the chooser just switches file view mode to that of the filename to be saved (if you don't understand, you just have to try it and see). This is likely due to the fact that the file its pointing to doesn't exist yet.

If the user changes the file name and tries to save, it works, but that defeats the whole point.

I was looking for a way to simply setText in the field, but it doesn't seem to have any intuitive access. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑丑阿 2024-08-11 11:32:29

对我来说效果很好。我修改了 Swing 教程中“如何使用文件选择器”中的 FileChooserDemo 示例,它正确显示了名称。

fc.setSelectedFile( new File("save.txt")); // added this line
int returnVal = fc.showSaveDialog(FileChooserDemo.this);

Works fine for me. I modified the FileChooserDemo example from the Swing tutorial on "How to Use File Choosers" and it displays the name properly.

fc.setSelectedFile( new File("save.txt")); // added this line
int returnVal = fc.showSaveDialog(FileChooserDemo.this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文