Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like you can listen to the <code>JFileChooser</code> for a change on the <code>FILE_FILTER_CHANGED_PROPERTY</code> property, then change the extension of the selected file appropriately using <code>setSelectedFile()</code>.</p> <hr> <p>EDIT: You're right, this solution doesn't work. It turns out that when the file filter is changed, the selected file is removed if its file type doesn't match the new filter. That's why you're getting the <code>null</code> when you try to <code>getSelectedFile()</code>.</p> <p>Have you considered adding the extension later? When I am writing a <code>JFileChooser</code>, I usually add the extension after the user has chosen a file to use and clicked "Save":</p> <pre><code>if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); String path = file.getAbsolutePath(); String extension = getExtensionForFilter(fileChooser.getFileFilter()); if(!path.endsWith(extension)) { file = new File(path + extension); } } </code></pre> <hr> <pre><code>fileChooser.addPropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { FileFilter filter = (FileFilter)evt.getNewValue(); String extension = getExtensionForFilter(filter); //write this method or some equivalent File selectedFile = fileChooser.getSelectedFile(); String path = selectedFile.getAbsolutePath(); path.substring(0, path.lastIndexOf(".")); fileChooser.setSelectedFile(new File(path + extension)); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload