Creating a file from a jFileChooser component
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
Read the section from the Swing tutorial on How to Use File Choosers. The demo code there works fine for me.
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, but getSelectedFiles
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
or fileChooser.showSaveDialog
were neither called yet or did not return JFileChooser.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.
精彩评论