Adjust selected File to FileFilter in a JFileChooser?

It looks like you can listen to the JFileChooser for a change on the FILE_FILTER_CHANGED_PROPERTY property, then change the extension of the selected file appropriately using setSelectedFile().

It looks like you can listen to the JFileChooser for a change on the FILE_FILTER_CHANGED_PROPERTY property, then change the extension of the selected file appropriately using setSelectedFile(). 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 null when you try to getSelectedFile(). Have you considered adding the extension later? When I am writing a JFileChooser, I usually add the extension after the user has chosen a file to use and clicked "Save": 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); } } 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)); } }).

You can also use a PropertyChangeListener on the SELECTED_FILE_CHANGED_PROPERTY prior to attaching your suffix. When the selected file gets checked against the new filter (and subsequently set to null), the SELECTED_FILE_CHANGED_PROPERTY event is actually fired before the FILE_FILTER_CHANGED_PROPERTY event. If the evt.getOldValue()!

= null and the evt.getNewValue() == null, you know that the JFileChooser has blasted your file. You can then grab the old file's name (using ((File)evt.getOldValue()).getName() as described above), pull off the extension using standard string parsing functions, and stash it into a named member variable within your class. That way, when the FILE_FILTER_CHANGED event is triggered (immediately afterwards, as near as I can determine), you can pull that stashed root name from the named member variable, apply the extension for the new file filter type, and set the JFileChooser's selected file accordingly.

The use of getAbsolutePath() in the previous change the current directory. I was surprised when the JFileChooser dialog displaying "My documents" directory change to the Netbeans's project directory when I selected a different FileFilter, so I changed it to use getName(). I also used the JDK 6 FileNameExtensionFilter.

Here is the code: final JFileChooser fc = new JFileChooser(); final File sFile = new File("test. Xls"); fc. SetSelectedFile(sFile); // Store this filter in a variable to be able to select this after adding all FileFilter // because addChoosableFileFilter add FileFilter in order in the combo box final FileNameExtensionFilter excelFilter = new FileNameExtensionFilter("Excel document (*.

Xls)", "xls"); fc. AddChoosableFileFilter(excelFilter); fc. AddChoosableFileFilter(new FileNameExtensionFilter("CSV document (*.

Csv)", "csv")); // Force the excel filter fc. SetFileFilter(excelFilter); // Disable All Files fc. SetAcceptAllFileFilterUsed(false); // debug fc.

AddPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { System.out. Println("Property name=" + evt.getPropertyName() + ", oldValue=" + evt.getOldValue() + ", newValue=" + evt.getNewValue()); System.out. Println("getSelectedFile()=" + fc.getSelectedFile()); } }); fc.

AddPropertyChangeListener(JFileChooser. FILE_FILTER_CHANGED_PROPERTY, new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { Object o = evt.getNewValue(); if (o instanceof FileNameExtensionFilter) { FileNameExtensionFilter filter = (FileNameExtensionFilter) o; String ex = filter.getExtensions()0; File selectedFile = fc.getSelectedFile(); if (selectedFile == null) { selectedFile = sFile; } String path = selectedFile.getName(); path = path. Substring(0, path.

LastIndexOf(". ")); fc. SetSelectedFile(new File(path + "." + ex)); } } }).

Here's my attempt at this. It uses the accept() function to check whether or not the file passes the filter. If the filename does not, the extension is appended to the end.

JFileChooser jfc = new JFileChooser(getFile()) { public void approveSelection() { if (getDialogType() == SAVE_DIALOG) { File selectedFile = getSelectedFile(); FileFilter ff = getFileFilter(); // Checks against the current selected filter if (!ff. Accept(selectedFile)) { selectedFile = new File(selectedFile.getPath() + ". Txt"); } super.

SetSelectedFile(selectedFile); if ((selectedFile! = null) && selectedFile.exists()) { int response = JOptionPane. ShowConfirmDialog( this, "The file " + selectedFile.getName() + " already exists.

\n" + "Do you want to replace it? ", "Ovewrite file", JOptionPane. YES_NO_OPTION, JOptionPane.

WARNING_MESSAGE ); if (response == JOptionPane. NO_OPTION) return; } } super. ApproveSelection(); } }.

How about this: class MyFileChooser extends JFileChooser { public void setFileFilter(FileFilter filter) { super. SetFileFilter(filter); FileChooserUI ui = getUI(); if( ui instanceof BasicFileChooserUI ) { BasicFileChooserUI bui = (BasicFileChooserUI) ui; String file = bui.getFileName(); if( file! = null ) { String newFileName = ... change extension bui.

SetFileName( newFileName ); } } } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions