使用 JFileChooser 并预先填充文件名保存?
我试图让我制作的一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说效果很好。我修改了 Swing 教程中“如何使用文件选择器”中的 FileChooserDemo 示例,它正确显示了名称。
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.