Java Code Examples for javax.swing.JFileChooser#getFileFilter()
The following examples show how to use
javax.swing.JFileChooser#getFileFilter() .
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: 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 2
Source File: MainForm.java From zxpoly with GNU General Public License v3.0 | 5 votes |
private void menuFileLoadPokeActionPerformed(ActionEvent evt) { stepSemaphor.lock(); try { this.turnZxKeyboardOff(); final JFileChooser trainerFileChooser = new JFileChooser(this.lastPokeFileFolder); trainerFileChooser.setDialogTitle("Select trainer"); trainerFileChooser.setMultiSelectionEnabled(false); trainerFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); trainerFileChooser.setAcceptAllFileFilterUsed(false); final FileFilter pokTrainer = new TrainerPok(); trainerFileChooser.addChoosableFileFilter(pokTrainer); if (trainerFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { final AbstractTrainer selectedTrainer = (AbstractTrainer) trainerFileChooser.getFileFilter(); final File selectedFile = trainerFileChooser.getSelectedFile(); this.lastPokeFileFolder = selectedFile.getParentFile(); try { selectedTrainer.apply(this, selectedFile, this.board); } catch (Exception ex) { LOGGER.log(Level.WARNING, "Error during trainer processing: " + ex.getMessage(), ex); JOptionPane.showMessageDialog(this, ex.getMessage(), "Can't read or parse file", JOptionPane.ERROR_MESSAGE); } } } finally { this.turnZxKeyboardOn(); stepSemaphor.unlock(); } }
Example 3
Source File: Utils.java From j-j-jvm with Apache License 2.0 | 5 votes |
public static File selectFileForOpen(final Component parent, final FileFilter[] fileFilters, final String title, final FileFilter[] selectedFilters, final File initFile) { JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(false); chooser.setDragEnabled(false); chooser.setControlButtonsAreShown(true); chooser.setDialogType(JFileChooser.OPEN_DIALOG); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); for (final FileFilter fileFilter : fileFilters) { chooser.addChoosableFileFilter(fileFilter); } chooser.setDialogTitle(title); chooser.setAcceptAllFileFilterUsed(false); if (initFile != null) { chooser.setCurrentDirectory(initFile); chooser.setName(initFile.getName()); } int returnVal = chooser.showDialog(parent, "Open"); selectedFilters[0] = chooser.getFileFilter(); if (returnVal == JFileChooser.APPROVE_OPTION) { File p_file = chooser.getSelectedFile(); return p_file; } else { return null; } }
Example 4
Source File: ExportColorPaletteAction.java From snap-desktop with GNU General Public License v3.0 | 5 votes |
@Override public void actionPerformed(ActionEvent e) { SnapFileFilter fileFilter1 = new SnapFileFilter("CSV", ".csv", "CSV files"); // I18N SnapFileFilter fileFilter2 = new SnapFileFilter("TXT", ".txt", "Text files"); // I18N JFileChooser fileChooser = new JFileChooser(); File lastDir = new File(getPreferences().getPropertyString(KEY_LAST_OPEN, ".")); fileChooser.setCurrentDirectory(lastDir); RasterDataNode raster = getSelectedRaster(); fileChooser.setSelectedFile(new File(lastDir, raster.getName() + "-palette.csv")); fileChooser.setAcceptAllFileFilterUsed(false); fileChooser.addChoosableFileFilter(fileFilter1); fileChooser.addChoosableFileFilter(fileFilter2); fileChooser.setFileFilter(fileFilter1); fileChooser.setMultiSelectionEnabled(true); fileChooser.setDialogTitle(Bundle.CTL_ExportColorPaletteAction_DialogTitle()); if (fileChooser.showSaveDialog(SnapApp.getDefault().getMainFrame()) == JFileChooser.APPROVE_OPTION && fileChooser.getSelectedFile() != null) { getPreferences().setPropertyString(KEY_LAST_OPEN, fileChooser.getCurrentDirectory().getAbsolutePath()); File file = fileChooser.getSelectedFile(); if (fileChooser.getFileFilter() instanceof SnapFileFilter) { SnapFileFilter fileFilter = (SnapFileFilter) fileChooser.getFileFilter(); file = FileUtils.ensureExtension(file, fileFilter.getDefaultExtension()); } try { writeColorPalette(raster, file); } catch (IOException ie) { Dialogs.showError(Bundle.CTL_ExportColorPaletteAction_DialogTitle(), "Failed to export colour palette:\n" + ie.getMessage()); } } }
Example 5
Source File: SikulixFileChooser.java From SikuliX1 with MIT License | 4 votes |
private void processDialog(int selectionMode, String last_dir, String title, int mode, Object[] filters, Object[] result) { JFileChooser fchooser = new JFileChooser(); File fileChoosen = null; FileFilter filterChoosen = null; if (!last_dir.isEmpty()) { fchooser.setCurrentDirectory(new File(last_dir)); } fchooser.setSelectedFile(null); fchooser.setDialogTitle(title); String btnApprove = "Select"; if (fromPopFile) { fchooser.setFileSelectionMode(DIRSANDFILES); fchooser.setAcceptAllFileFilterUsed(true); } else { fchooser.setFileSelectionMode(selectionMode); if (mode == FileDialog.SAVE) { fchooser.setDialogType(JFileChooser.SAVE_DIALOG); btnApprove = "Save"; } if (filters.length == 0) { fchooser.setAcceptAllFileFilterUsed(true); } else { fchooser.setAcceptAllFileFilterUsed(false); for (Object filter : filters) { fchooser.setFileFilter((FileFilter) filter); } } } if (Settings.isMac()) { fchooser.putClientProperty("JFileChooser.packageIsTraversable", "always"); } int dialogResponse = fchooser.showDialog(parentFrame, btnApprove); if (dialogResponse != JFileChooser.APPROVE_OPTION) { fileChoosen = null; } else { fileChoosen = fchooser.getSelectedFile(); } result[0] = fileChoosen; if (filters.length > 0) { result[1] = fchooser.getFileFilter(); } }
Example 6
Source File: ExportAction.java From netbeans with Apache License 2.0 | 4 votes |
private SelectedFile selectExportTargetFile(final ExportProvider exportProvider) { File targetDir; String targetName; String defaultName = exportProvider.getViewName(); // 1. let the user choose file or directory final JFileChooser chooser = getFileChooser(); if (exportDir != null) { chooser.setCurrentDirectory(exportDir); } int result = chooser.showSaveDialog(WindowManager.getDefault().getRegistry().getActivated()); if (result != JFileChooser.APPROVE_OPTION) { return null; // cancelled by the user } // 2. process both cases and extract file name and extension to use and set exported file type File file = chooser.getSelectedFile(); String targetExt = null; FileFilter selectedFileFilter = chooser.getFileFilter(); if (selectedFileFilter==null // workaround for #227659 || selectedFileFilter.getDescription().equals(Bundle.ExportAction_ExportDialogCSVFilter())) { targetExt=FILE_EXTENSION_CSV; exportedFileType=MODE_CSV; } else if (selectedFileFilter.getDescription().equals(Bundle.ExportAction_ExportDialogTXTFilter())) { targetExt=FILE_EXTENSION_TXT; exportedFileType=MODE_TXT; } else if (selectedFileFilter.getDescription().equals(Bundle.ExportAction_ExportDialogBINFilter())) { targetExt=FILE_EXTENSION_BIN; exportedFileType=MODE_BIN; } if (file.isDirectory()) { // save to selected directory under default name exportDir = file; targetDir = file; targetName = defaultName; } else { // save to selected file targetDir = fileChooser.getCurrentDirectory(); String fName = file.getName(); // divide the file name into name and extension if (fName.endsWith("."+targetExt)) { // NOI18N int idx = fName.lastIndexOf('.'); // NOI18N targetName = fName.substring(0, idx); } else { // no extension targetName=fName; } } // 3. set type of exported file and return a newly created FileObject return new ExportAction.SelectedFile(targetDir, targetName, targetExt); }
Example 7
Source File: OpenFileAction.java From netbeans with Apache License 2.0 | 4 votes |
/** * {@inheritDoc} Displays a file chooser dialog * and opens the selected files. */ @Override public void actionPerformed(ActionEvent e) { if (running) { return; } try { running = true; JFileChooser chooser = prepareFileChooser(); File[] files; try { if( Boolean.getBoolean("nb.native.filechooser") ) { //NOI18N String oldFileDialogProp = System.getProperty("apple.awt.fileDialogForDirectories"); //NOI18N System.setProperty("apple.awt.fileDialogForDirectories", "false"); //NOI18N FileDialog fileDialog = new FileDialog(WindowManager.getDefault().getMainWindow()); fileDialog.setMode(FileDialog.LOAD); fileDialog.setDirectory(getCurrentDirectory().getAbsolutePath()); fileDialog.setTitle(chooser.getDialogTitle()); fileDialog.setVisible(true); if( null != oldFileDialogProp ) { System.setProperty("apple.awt.fileDialogForDirectories", oldFileDialogProp); //NOI18N } else { System.clearProperty("apple.awt.fileDialogForDirectories"); //NOI18N } if( fileDialog.getDirectory() != null && fileDialog.getFile() != null ) { String selFile = fileDialog.getFile(); File dir = new File( fileDialog.getDirectory() ); files = new File[] { new File( dir, selFile ) }; currentDirectory = dir; } else { throw new UserCancelException(); } } else { files = chooseFilesToOpen(chooser); currentDirectory = chooser.getCurrentDirectory(); if (chooser.getFileFilter() != null) { // #227187 currentFileFilter = chooser.getFileFilter().getDescription(); } } } catch (UserCancelException ex) { return; } for (int i = 0; i < files.length; i++) { OpenFile.openFile(files[i], -1); } } finally { running = false; } }
Example 8
Source File: ExportAction.java From visualvm with GNU General Public License v2.0 | 4 votes |
private SelectedFile selectExportTargetFile(final ExportProvider exportProvider) { File targetDir; String targetName; String defaultName = exportProvider.getViewName(); // 1. let the user choose file or directory final JFileChooser chooser = getFileChooser(); if (exportDir != null) { chooser.setCurrentDirectory(exportDir); } int result = chooser.showSaveDialog(WindowManager.getDefault().getRegistry().getActivated()); if (result != JFileChooser.APPROVE_OPTION) { return null; // cancelled by the user } // 2. process both cases and extract file name and extension to use and set exported file type File file = chooser.getSelectedFile(); String targetExt = null; FileFilter selectedFileFilter = chooser.getFileFilter(); if (selectedFileFilter==null // workaround for #227659 || selectedFileFilter.getDescription().equals(Bundle.ExportAction_ExportDialogCSVFilter())) { targetExt=FILE_EXTENSION_CSV; exportedFileType=MODE_CSV; } else if (selectedFileFilter.getDescription().equals(Bundle.ExportAction_ExportDialogTXTFilter())) { targetExt=FILE_EXTENSION_TXT; exportedFileType=MODE_TXT; } else if (selectedFileFilter.getDescription().equals(Bundle.ExportAction_ExportDialogBINFilter())) { targetExt=FILE_EXTENSION_BIN; exportedFileType=MODE_BIN; } if (file.isDirectory()) { // save to selected directory under default name exportDir = file; targetDir = file; targetName = defaultName; } else { // save to selected file targetDir = fileChooser.getCurrentDirectory(); String fName = file.getName(); // divide the file name into name and extension if (fName.endsWith("."+targetExt)) { // NOI18N int idx = fName.lastIndexOf('.'); // NOI18N targetName = fName.substring(0, idx); } else { // no extension targetName=fName; } } // 3. set type of exported file and return a newly created FileObject return new ExportAction.SelectedFile(targetDir, targetName, targetExt); }