使用带有预填充文件名的JFileChooser保存吗? [英] Save using JFileChooser with pre-populated file name?

查看:91
本文介绍了使用带有预填充文件名的JFileChooser保存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试简化我制作的某些GUI的保存和加载,我希望能够在保存时为用户预填充文件名.

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.

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

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()));

这实际上似乎起初是可行的:在JFileChooser中填充了文件名,但是当单击保存"按钮时,选择器只是将文件查看模式切换到要保存的文件名的模式(如果您不了解,您会只需尝试一下就可以看到).这可能是由于其指向的文件尚不存在.

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.

我一直在寻找一种简单地在字段中设置setText的方法,但是它似乎没有任何直观的访问权限.有什么想法吗?

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

推荐答案

对我来说很好.我修改了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.

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

这篇关于使用带有预填充文件名的JFileChooser保存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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