Java Code Examples for javax.swing.filechooser.FileNameExtensionFilter#getExtensions()
The following examples show how to use
javax.swing.filechooser.FileNameExtensionFilter#getExtensions() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SynchronizedFileChooser.java From openAGV with Apache License 2.0 | 5 votes |
private void formPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_formPropertyChange //Synchronize if the user selects a different filter if (evt.getPropertyName().equals(FILE_FILTER_CHANGED_PROPERTY)) { if (selectedFileName == null || (getSelectedFile() != null && selectedFileName.equals(getSelectedFile().getName()))) { return; } File file = new File(getCurrentDirectory(), selectedFileName); //We currently have no file selected so we dont need to modify it //The equals check ensures that both events wont trigger each other over and over Object filter = evt.getNewValue(); if (filter instanceof FileNameExtensionFilter) { FileNameExtensionFilter extensionFilter = (FileNameExtensionFilter) filter; int fileExtensionIndex = getFileExtensionIndex(file); //Select the first extension of the filter as the new file extension String newExtension = extensionFilter.getExtensions()[0]; //If the file has a known file ending, replace it. Else just add the new extension //at the end StringBuilder newPathBuilder = new StringBuilder(); newPathBuilder.append(fileExtensionIndex >= 0 ? file.getName().substring(0, fileExtensionIndex) : file.getName()); if (!newPathBuilder.toString().endsWith(".")) { newPathBuilder.append("."); } newPathBuilder.append(newExtension); //Update the chosen file setSelectedFile(new File(file.getParent(), newPathBuilder.toString())); } } //Synchronize if the user selects a different file else if (evt.getPropertyName().equals(SELECTED_FILE_CHANGED_PROPERTY)) { Object newFile = evt.getNewValue(); if (newFile instanceof File) { this.selectedFileName = ((File) newFile).getName(); } } }
Example 2
Source File: SynchronizedFileChooser.java From openAGV with Apache License 2.0 | 5 votes |
/** * Returns the index of the file extension in the file name if the current file ending is * known by one of the file name extension filters. * * @param file The file * @return The index of a known file extension in the file's name or <code>-1</code> */ private int getFileExtensionIndex(File file) { for (FileFilter filter : getChoosableFileFilters()) { if (filter instanceof FileNameExtensionFilter) { FileNameExtensionFilter extensionFilter = (FileNameExtensionFilter) filter; for (String extension : extensionFilter.getExtensions()) { if (file.getName().endsWith(extension)) { return file.getName().length() - extension.length(); } } } } return -1; }
Example 3
Source File: SaveFileChooserDialog.java From pgptool with GNU General Public License v3.0 | 5 votes |
private String enforceExtension(String filePathName, JFileChooser ofd) { FileFilter fileExtFilter = ofd.getFileFilter(); if (fileExtFilter == ofd.getAcceptAllFileFilter()) { return filePathName; } FileNameExtensionFilter fileNameExtensionFilter = (FileNameExtensionFilter) fileExtFilter; String ext = fileNameExtensionFilter.getExtensions()[0]; if (!ext.equalsIgnoreCase(FilenameUtils.getExtension(filePathName))) { filePathName += "." + ext; } return filePathName; }
Example 4
Source File: ZipDialog.java From Compressor with GNU General Public License v2.0 | 5 votes |
private void onCompressFile(Compressor c) { File f = getSelectedArchiverFile(null); if (f == null) { return; } FileNameExtensionFilter filter = c.getFileFilter(); String ext = "." + filter.getExtensions()[0]; String destpath = f.getName() + ext; JFileChooser s = new JFileChooser("."); s.addChoosableFileFilter(filter); s.setSelectedFile(new File(destpath)); int returnVal = s.showSaveDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { return; } File ff = s.getSelectedFile(); destpath = ff.getAbsolutePath(); if (!filter.accept(ff)) {// 确保一定有后缀 destpath += ext; } try { c.doCompress(f, destpath); } catch (IOException e) { e.printStackTrace(); } }
Example 5
Source File: ZipDialog.java From Compressor with GNU General Public License v2.0 | 5 votes |
private void onArchiverFile(Archiver ma) { JFileChooser o = new JFileChooser("."); o.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); o.setMultiSelectionEnabled(true); int returnVal = o.showOpenDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { return; } File[] files = o.getSelectedFiles(); JFileChooser s = new JFileChooser("."); FileNameExtensionFilter filter = ma.getFileFilter(); s.addChoosableFileFilter(filter); returnVal = s.showSaveDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { return; } File f = s.getSelectedFile(); String filepath = f.getAbsolutePath(); if (!filter.accept(f)) {// 确保一定有后缀 filepath = filepath + "." + filter.getExtensions()[0]; } try { ma.doArchiver(files, filepath); } catch (IOException e) { e.printStackTrace(); } }