I use JFileChooser as part of the export function. I would like the user to either select a file from the JFileChooser viewer file, or enter the file name in the text box of the file name. From what I read, you can get this value using the getSelectedFile() method, so I have several listeners that call getSelectedFile() and try to perform some checks before doing the export.
The problem I am facing is that the getSelectedFile() method returns null when entering the name in the text box of the file name manually. To add more confusion, the getSelectedFile() method works in three different situations:
- I populate it through
setSelectedFile() (the user clicked the value from the table and I use setSelectedFile() ) - I click on an existing file in the file viewer
- I press ENTER after filling in the text field of the file name.
I have three file filters, but they have the same behavior regardless of whether they are enabled or not.
The listeners calling getSelectedFile() are as follows:
- Event Listener for keyReleased
- Event listener for mousePressed.
- PropertyChangeEvent listener on my jFileChooser
- Event listener on my jFileChooser
Is there a better way to listen to my jFileChooser to get user input? I feel like I'm missing something very obvious ... any help is appreciated!
change A little more information ...
I have a JFileChooser component in JSplitPane that is in a JFrame . I do not call showOpenDialog to get input from the user. The component is available as part of the form.
What I would like to do is listen to user input like he does. I have a start export button that I would like to leave disabled until the user enters a valid file name in the file name text box in the JFileChooser component. For this, I have a KeyEvent listener that I would like to use to get the file name when the user enters it.
edit further
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:
jFileChooserExport.addPropertyChangeListener(new java.beans.PropertyChangeListener() { public void propertyChange(java.beans.PropertyChangeEvent evt) { jFileChooserExportPropertyChange(evt); } });
Inside jFileChooserExportPropertyChange and jFileChooserExportActionPerformed I'm trying to get the file that the user selected by calling getSelectedFile (). However, in both cases, it remains zero until the user executes one of the three methods described above.