Creating a file from jFileChooser component

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.

+4
source share
2 answers

Read the section in the Swing tutorial on How to Use File Choosers . The demo code there works great for me.

+2
source

Since none of the actions below work, you can try adding a PropertyChangeListener to your JFileChooser by listening to SELECTED_FILE_CHANGED_PROPERTY


What might be possible is that your file selector may include multiple selections , in which case getSelectedFile will return null, but getSelectedFiles will return an array containing the selected file (s). You can either choose to disable multiple selection or use an array (if you want the user to select only one file, set the multiSelectionEnabled parameter to false).

Another possibility, however, is that if you try to get the selected file, but fileChooser.showOpenDialog or fileChooser.showSaveDialog have not yet been called or returned JFileChooser.APPROVE_OPTION

Also, I think JFileChooser is case sensitive, so if the file name is โ€œFoo.barโ€ โ€‹โ€‹and you enter โ€œFoO.barโ€, it will think that you need something else.

0
source

Source: https://habr.com/ru/post/1339056/


All Articles