从 jFileChooser 组件创建文件
我使用 JFileChooser
作为导出功能的一部分。我希望用户能够从 JFileChooser 的文件查看器中选择文件或在文件名文本框中输入文件名。根据我的阅读,可以使用 getSelectedFile() 方法获取该值,因此我有一些侦听器调用 getSelectedFile() 并尝试在之前进行一些检查执行导出。
我遇到的问题是,当我手动在文件名文本框中输入名称时,getSelectedFile()
方法返回 null。为了增加更多混乱,getSelectedFile()
方法在三种不同的情况下工作:
- 我通过
setSelectedFile()
填充它(用户单击了表中的值,然后我使用setSelectedFile()
) - 我在文件查看器中单击一个现有文件
- 在填充文件名文本框后按 ENTER
我有三个文件过滤器,但无论它们是否启用,都具有相同的行为。
调用 getSelectedFile() 的监听器如下:
- keyReleased 的事件监听器
- mousePressed 的事件监听器。
- jFileChooser 上的 PropertyChangeEvent 侦听器
- jFileChooser 上的操作侦听器
有没有更好的方法来侦听我的 jFileChooser 来获取用户输入?我觉得我错过了一些非常明显的东西......感谢任何帮助!
编辑 更多信息...
我在 JSplitPane
中有一个 JFileChooser
组件,它位于 JFrame
中。我没有调用 showOpenDialog
来获取用户的输入。该组件可以作为表单的一部分进行访问。
我想做的是在用户输入时监听他/她的输入。我有一个“开始导出”按钮,我希望将其保持禁用状态,直到用户在 JFileChooser
组件的文件名文本框中输入有效的文件名。为了实现这一点,我有一个 KeyEvent 侦听器,我想用它来获取用户键入的文件名。
进一步编辑
这是操作侦听器代码:
jFileChooserExport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jFileChooserExportActionPerformed(evt);
}
});
我这里还有一个属性更改侦听器:
jFileChooserExport.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
jFileChooserExportPropertyChange(evt);
}
});
在 jFileChooserExportPropertyChange 和 jFileChooserExportActionPerformed 内部,我尝试通过调用 getSelectedFile() 来获取用户选择的文件。然而,在这两种情况下,它都保持为空,直到用户执行上述三种方法之一
I am using JFileChooser
as part of an export feature. I would like for the user to be able to either select a file from JFileChooser
's file viewer or enter the name of a file in the filename text box. From what I've read it's possible to get that value using the getSelectedFile()
method, so I have some listeners that call getSelectedFile()
and attempt to do some checks before executing the export.
The problem I'm encountering is that the getSelectedFile()
method is returning null when I enter the name into the filename text box manually. To add more confusion, the getSelectedFile()
method does work in three different situations:
- I populate it via
setSelectedFile()
(a user has clicked a value from a table and I usesetSelectedFile()
) - I click an existing file in the file viewer
- I hit ENTER after populating the filename text box
I have three file filters but have had the same behavior regardless of if they are enabled or not.
Listeners that call getSelectedFile()
are as follows:
- Event Listener for keyReleased
- Event Listener for mousePressed.
- PropertyChangeEvent listener on my jFileChooser
- Action Listener on my jFileChooser
Is there a better way to listen to my jFileChooser to get the user input? I feel like I'm missing something very obvious ... any help is appreciated!
edit
A little more info ...
I have a JFileChooser
component in a JSplitPane
, which is in a JFrame
. I'm not calling showOpenDialog
to get input from the user. The component is accessible as part of the form.
What I would like to do is listen to the user input as he/she types. I have a 'start export' button that I would like to leave disabled until the user has entered a valid filename in the filename textbox in the JFileChooser
component. To accomplish this I have a KeyEvent listener that I would like to use to get the filename as the user types it in.
further edit
Here is the action listener code:
jFileChooserExport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jFileChooserExportActionPerformed(evt);
}
});
I also have a property change listener here:
jFileChooserExport.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
jFileChooserExportPropertyChange(evt);
}
});
Inside of both jFileChooserExportPropertyChange and jFileChooserExportActionPerformed I am trying to get the file the user has selected by invoking getSelectedFile(). In both cases, however, it remains null until the user does one of the three methods described above
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读 Swing 教程中有关如何使用文件选择器的部分。那里的演示代码对我来说效果很好。
Read the section from the Swing tutorial on How to Use File Choosers. The demo code there works fine for me.
由于以下似乎都不起作用,您可能需要尝试 添加一个 PropertyChangeListener 到您的 JFileChooser,监听 SELECTED_FILE_CHANGED_PROPERTY
可能发生的情况是您的文件选择器可能有 启用多选,在这种情况下
getSelectedFile
将返回 null,但getSelectedFiles
将返回一个包含所选文件的数组文件。您可能想要禁用多项选择,或者使用数组(如果您希望用户只选择一个文件,请将 multiSelectionEnabled 设置为 false)。不过,另一种可能性是,如果您尝试获取所选文件,但
fileChooser.showOpenDialog
或fileChooser.showSaveDialog
尚未调用或未返回JFileChooser.APPROVE_OPTION
另外,我相信 JFileChooser 区分大小写,因此如果文件名是“Foo.bar”并且您输入“FoO.bar”,它会认为您想要其他内容。
Since none of below seems to work, you might want to try to add a PropertyChangeListener to your JFileChooser, listening for the SELECTED_FILE_CHANGED_PROPERTY
What might be possibly happening is that your file chooser may have multi selection enabled, in which case
getSelectedFile
will return null, butgetSelectedFiles
will return an array containing the selected file(s). You may either want to disable multi selection, or use the array (If you want the user to only select one file, set multiSelectionEnabled to false).Another possibility, though, is if you try to get the selected file but
fileChooser.showOpenDialog
orfileChooser.showSaveDialog
were neither called yet or did not returnJFileChooser.APPROVE_OPTION
Also, I believe JFileChooser is case-sensitive, so if the file name is "Foo.bar" and you enter "FoO.bar", it will think you want something else.