Java Code Examples for javax.swing.JFileChooser#addChoosableFileFilter()

The following examples show how to use javax.swing.JFileChooser#addChoosableFileFilter() . 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: LandUseBalloonPanel.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
private void loadFile() {
	JFileChooser fileChooser = new JFileChooser();

	FileNameExtensionFilter filter = new FileNameExtensionFilter("HTML Files (*.htm, *.html)", "htm", "html");
	fileChooser.addChoosableFileFilter(filter);
	fileChooser.addChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
	fileChooser.setFileFilter(filter);

	if (internalBalloon.getBalloonContentPath().isSetLastUsedMode()) {
		fileChooser.setCurrentDirectory(new File(internalBalloon.getBalloonContentPath().getLastUsedPath()));
	} else {
		fileChooser.setCurrentDirectory(new File(internalBalloon.getBalloonContentPath().getStandardPath()));
	}
	int result = fileChooser.showSaveDialog(getTopLevelAncestor());
	if (result == JFileChooser.CANCEL_OPTION) return;
	try {
		String exportString = fileChooser.getSelectedFile().toString();
		browseText.setText(exportString);
		internalBalloon.getBalloonContentPath().setLastUsedPath(fileChooser.getCurrentDirectory().getAbsolutePath());
		internalBalloon.getBalloonContentPath().setPathMode(PathMode.LASTUSED);
	}
	catch (Exception e) {
		//
	}
}
 
Example 2
Source File: MainFrame.java    From FCMFrame with Apache License 2.0 6 votes vote down vote up
public JFileChooser saveFileChooser() {
  	JFileChooser saveFc = new JFileChooser();
  	File swingFile = new File(OLD_FILEPATH);
  	if(swingFile.exists()) {
  		saveFc.setCurrentDirectory(swingFile);
  	    saveFc.setSelectedFile(swingFile);
  	}
saveFc.addChoosableFileFilter(new FileFilter(){
  		public boolean accept(File f) {
  			if (f.isDirectory()) {
  				return true;
  			}
  			if (f.getName().endsWith(".csv")) {
  				return true;
  			}
  			return false;
  		}
  		public String getDescription() {
  			return "*.csv";
  		}
});
return saveFc;
  }
 
Example 3
Source File: frmMain.java    From Course_Generator with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Display a dialog box to open a CGX file
 */
private void OpenCGXDialog() {
	JFileChooser fileChooser = new JFileChooser();
	fileChooser.setCurrentDirectory(new File(Settings.previousCGXDirectory)); // System.getProperty("user.home")));
	fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

	FileFilter cgxFilter = new FileTypeFilter(".cgx", bundle.getString("frmMain.CGXFile")); // "CGX
																							// file");
	fileChooser.addChoosableFileFilter(cgxFilter);
	fileChooser.setFileFilter(cgxFilter);

	int result = fileChooser.showOpenDialog(this);
	if (result == JFileChooser.APPROVE_OPTION) {
		File selectedFile = fileChooser.getSelectedFile();
		LoadCGX(selectedFile.getAbsolutePath());
		Settings.previousCGXDirectory = Utils.GetDirFromFilename(selectedFile.getAbsolutePath());
	}
}
 
Example 4
Source File: ChartPanel.java    From SIMVA-SoS with Apache License 2.0 6 votes vote down vote up
/**
 * Opens a file chooser and gives the user an opportunity to save the chart
 * in PNG format.
 *
 * @throws IOException if there is an I/O error.
 */
public void doSaveAs() throws IOException {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setCurrentDirectory(this.defaultDirectoryForSaveAs);
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
                localizationResources.getString("PNG_Image_Files"), "png");
    fileChooser.addChoosableFileFilter(filter);
    fileChooser.setFileFilter(filter);

    int option = fileChooser.showSaveDialog(this);
    if (option == JFileChooser.APPROVE_OPTION) {
        String filename = fileChooser.getSelectedFile().getPath();
        if (isEnforceFileExtensions()) {
            if (!filename.endsWith(".png")) {
                filename = filename + ".png";
            }
        }
        ChartUtilities.saveChartAsPNG(new File(filename), this.chart,
                getWidth(), getHeight());
    }
}
 
Example 5
Source File: IdeSnapshotAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@NbBundle.Messages({
    "ACTION_IdeSnapshot_dialog=Load IDE Snapshot",
    "ACTION_IdeSnapshot_filter=IDE Snapshots"
})
private static JFileChooser createFileChooser(File directory) {
    JFileChooser chooser = new JFileChooser();

    chooser.setDialogTitle(Bundle.ACTION_IdeSnapshot_dialog());
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

    chooser.setAcceptAllFileFilterUsed(false);
    if (directory != null) {
        chooser.setCurrentDirectory(directory);
    }
    
    String descr = Bundle.ACTION_IdeSnapshot_filter();
    String ext = "."+ResultsManager.STACKTRACES_SNAPSHOT_EXTENSION; // NOI18N
    Filter filter = Filter.create(descr, ext);
    chooser.addChoosableFileFilter(filter);

    return chooser;
}
 
Example 6
Source File: ChartPanel.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Opens a file chooser and gives the user an opportunity to save the chart
 * in PNG format.
 *
 * @throws IOException if there is an I/O error.
 */
public void doSaveAs() throws IOException {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setCurrentDirectory(this.defaultDirectoryForSaveAs);
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
                localizationResources.getString("PNG_Image_Files"), "png");
    fileChooser.addChoosableFileFilter(filter);
    fileChooser.setFileFilter(filter);

    int option = fileChooser.showSaveDialog(this);
    if (option == JFileChooser.APPROVE_OPTION) {
        String filename = fileChooser.getSelectedFile().getPath();
        if (isEnforceFileExtensions()) {
            if (!filename.endsWith(".png")) {
                filename = filename + ".png";
            }
        }
        ChartUtilities.saveChartAsPNG(new File(filename), this.chart,
                getWidth(), getHeight());
    }
}
 
Example 7
Source File: MainForm.java    From zxpoly with GNU General Public License v3.0 6 votes vote down vote up
private File chooseFileForSave(final String title, final File initial,
                               final AtomicReference<FileFilter> selectedFilter,
                               final boolean allowAcceptAll, final FileFilter... filters) {
  final JFileChooser chooser = new JFileChooser(initial);
  for (final FileFilter f : filters) {
    chooser.addChoosableFileFilter(f);
  }
  chooser.setAcceptAllFileFilterUsed(allowAcceptAll);
  chooser.setMultiSelectionEnabled(false);
  chooser.setDialogTitle(title);
  chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

  final File result;
  if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
    result = chooser.getSelectedFile();
    if (selectedFilter != null) {
      selectedFilter.set(chooser.getFileFilter());
    }
  } else {
    result = null;
  }
  return result;
}
 
Example 8
Source File: AddServerPropertiesVisualPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private JFileChooser getJFileChooser(){
    JFileChooser chooser = new JFileChooser();
    
    chooser.setDialogTitle("LBL_Chooser_Name"); //NOI18N
    chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
    
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setApproveButtonMnemonic("Choose_Button_Mnemonic".charAt(0)); //NOI18N
    chooser.setMultiSelectionEnabled(false);
    chooser.addChoosableFileFilter(new dirFilter());
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.setApproveButtonToolTipText("LBL_Chooser_Name"); //NOI18N
    
    chooser.getAccessibleContext().setAccessibleName("LBL_Chooser_Name"); //NOI18N
    chooser.getAccessibleContext().setAccessibleDescription("LBL_Chooser_Name"); //NOI18N
    
    return chooser;
}
 
Example 9
Source File: OperatorMenu.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent event) {
    JFileChooser fileChooser = new JFileChooser();
    final FileNameExtensionFilter parameterFileFilter = createParameterFileFilter();
    fileChooser.addChoosableFileFilter(parameterFileFilter);
    fileChooser.setFileFilter(parameterFileFilter);
    fileChooser.setDialogTitle(TITLE);
    fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
    applyCurrentDirectory(fileChooser);
    int response = fileChooser.showDialog(parentComponent, "Save");
    if (JFileChooser.APPROVE_OPTION == response) {
        try {
            preserveCurrentDirectory(fileChooser);
            File selectedFile = fileChooser.getSelectedFile();
            selectedFile = FileUtils.ensureExtension(selectedFile,
                                                     "." + parameterFileFilter.getExtensions()[0]);
            DomElement domElement = parameterSupport.toDomElement();
            escapeXmlElements(domElement);
            String xmlString = domElement.toXml();
            writeToFile(xmlString, selectedFile);
        } catch (Exception e) {
            Debug.trace(e);
            Dialogs.showError(TITLE, "Could not load parameters.\n" + e.getMessage());
        }
    }
}
 
Example 10
Source File: Dialogos.java    From brModelo with GNU General Public License v3.0 5 votes vote down vote up
public static File ShowDlgSaveAsImg(JComponent form, Diagrama diag) {
    JFileChooser f = new JFileChooser();
    //f.setDialogTitle(Editor.fromConfiguracao.getValor("Controler.dlg.modelo.salvar"));
    f.setFileSelectionMode(JFileChooser.FILES_ONLY);

    FileFilter filter = new FileNameExtensionFilter("Imagem (png)", Arquivo.png);
    FileFilter filter2 = new FileNameExtensionFilter("Imagem (bmp)", Arquivo.bmp);
    f.addChoosableFileFilter(filter);
    f.addChoosableFileFilter(filter2);
    f.setAcceptAllFileFilterUsed(false);
    f.setFileFilter(filter);
    if (dir.isEmpty()) dir = System.getProperty("user.dir");
    f.setCurrentDirectory(new File(dir));
    f.setDialogTitle(Editor.fromConfiguracao.getValor("Controler.MSG_EPRT_TITLE"));
    if (!diag.getNome().isEmpty()){ 
        f.setSelectedFile(new File(diag.getNome()));
    }
    //f.setApproveButtonText(Editor.fromConfiguracao.getValor("Controler.dlg.modelo.salvar"));
    int returnVal = f.showSaveDialog(form);
    //int returnVal = f.showDialog(form, Editor.fromConfiguracao.getValor("Controler.dlg.modelo.salvar"));

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = f.getSelectedFile();
        String ext = Arquivo.getExtension(file);
        if (ext == null) {
            ext = "";
        }
        dir = file.getAbsolutePath();
        if (f.getFileFilter().equals(filter) && !Arquivo.png.toUpperCase().equals(ext.toUpperCase())) {
            return new File(file.getAbsolutePath() + "." + Arquivo.png);
        }
        if (f.getFileFilter().equals(filter2) && !Arquivo.bmp.toUpperCase().equals(ext.toUpperCase())) {
            return new File(file.getAbsolutePath() + "." + Arquivo.bmp);
        }
        return file;
    } else {
        return null;
    }
}
 
Example 11
Source File: MPrinter.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * Choix du fichier pour un export.
 *
 * @return File
 * @param table
 *           JTable
 * @param extension
 *           String
 * @throws IOException
 *            Erreur disque
 */
protected File chooseFile(final JTable table, final String extension) throws IOException {
	final JFileChooser myFileChooser = getFileChooser();

	final MExtensionFileFilter filter = new MExtensionFileFilter(extension);
	myFileChooser.addChoosableFileFilter(filter);

	String title = buildTitle(table);
	if (title != null) {
		// on remplace par des espaces les caractères interdits dans les noms de fichiers : \ / : * ? " < > |
		final String notAllowed = "\\/:*?\"<>|";
		final int notAllowedLength = notAllowed.length();
		for (int i = 0; i < notAllowedLength; i++) {
			title = title.replace(notAllowed.charAt(i), ' ');
		}
		myFileChooser.setSelectedFile(new File(title));
	}
	// l'extension sera ajoutée ci-dessous au nom du fichier

	try {
		final Component parent = table.getParent() != null ? table.getParent() : table;
		if (myFileChooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) {
			String fileName = myFileChooser.getSelectedFile().getCanonicalPath();
			if (!fileName.endsWith('.' + extension)) {
				fileName += '.' + extension; // NOPMD
			}

			return new File(fileName);
		}
		return null;
	} finally {
		myFileChooser.removeChoosableFileFilter(filter);
	}
}
 
Example 12
Source File: FileChooserFactory.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get a JFileChooser filtered for KeyStore files.
 *
 * @return JFileChooser object
 */
public static JFileChooser getKeyStoreFileChooser() {
	JFileChooser chooser = getFileChooser();
	chooser.addChoosableFileFilter(
			new FileExtFilter(new String[] { PKCS12_KEYSTORE_EXT_1, PKCS12_KEYSTORE_EXT_2 }, PKCS12_FILE_DESC));
	chooser.addChoosableFileFilter(new FileExtFilter(
			new String[] { KEYSTORE_EXT_1, KEYSTORE_EXT_2, JKS_EXT, JCEKS_EXT, BKS_EXT, UBER_EXT, BCFKS_EXT },
			KEYSTORE_FILE_DESC));
	return chooser;
}
 
Example 13
Source File: OpenAction.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
public static JFileChooser createFileChooser(ScriptEngineFactory scriptEngineFactory) {
    JFileChooser fs = new JFileChooser();
    fs.setAcceptAllFileFilterUsed(false);
    FileNameExtensionFilter filter = createFileFilter(scriptEngineFactory);
    fs.addChoosableFileFilter(filter);
    return fs;
}
 
Example 14
Source File: MaskFormActions.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private void importMasks() {
    final JFileChooser fileChooser = new SnapFileChooser();
    fileChooser.setDialogTitle("Import Masks from file");
    final FileFilter bmdFilter = new SnapFileFilter("BITMASK_DEFINITION_FILE", ".bmd",
                                                    "Bitmask definition files (*.bmd)");
    fileChooser.addChoosableFileFilter(bmdFilter);
    final FileFilter bmdxFilter = new SnapFileFilter("BITMASK_DEFINITION_FILE_XML", ".bmdx",
                                                     "Bitmask definition xml files (*.bmdx)");
    fileChooser.addChoosableFileFilter(bmdxFilter);
    final FileFilter xmlFilter = new SnapFileFilter("XML", ".xml", "XML files (*.xml)");
    fileChooser.setFileFilter(xmlFilter);
    fileChooser.setCurrentDirectory(getDirectory());
    fileChooser.setMultiSelectionEnabled(true);
    if (fileChooser.showOpenDialog(SwingUtilities.getWindowAncestor(maskTopComponent)) == JFileChooser.APPROVE_OPTION) {
        final File file = fileChooser.getSelectedFile();
        if (file != null) {
            setDirectory(file.getAbsoluteFile().getParentFile());
            if (file.canRead()) {
                if (bmdFilter.accept(file)) {
                    importMaskFromBmd(file);
                } else if (bmdxFilter.accept(file)) {
                    importMasksFromBmdx(file);
                } else {
                    importMasksFromXml(file);
                }

            }
        }
    }
}
 
Example 15
Source File: SelectAllFilesFilterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void createAndShowGUI() {
    fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(true);
    fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);

    FileFilter txtFilter = new FileNameExtensionFilter("Text files", "txt");
    fileChooser.addChoosableFileFilter(txtFilter);
    fileChooser.setFileFilter(txtFilter);
    fileChooser.showOpenDialog(null);
}
 
Example 16
Source File: DialogProviderManager.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public File msgSaveFileDialog(@Nullable final Component parentComponent, @Nonnull final String id, @Nonnull final String title, @Nullable final File defaultFolder, final boolean filesOnly, @Nonnull @MustNotContainNull final FileFilter[] fileFilters, @Nonnull final String approveButtonText) {
  final File folderToUse;
  if (defaultFolder == null) {
    folderToUse = cacheSaveFileThroughDialog.find(null, id);
  } else {
    folderToUse = defaultFolder;
  }

  final JFileChooser fileChooser = new JFileChooser(folderToUse);
  fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
  fileChooser.setDialogTitle(title);
  fileChooser.setApproveButtonText(approveButtonText);
  fileChooser.setAcceptAllFileFilterUsed(true);
  for (final FileFilter f : fileFilters) {
    fileChooser.addChoosableFileFilter(f);
  }
  if (fileFilters.length != 0) {
    fileChooser.setFileFilter(fileFilters[0]);
  }
  fileChooser.setMultiSelectionEnabled(false);

  File result = null;
  if (fileChooser.showDialog(GetUtils.ensureNonNull(
      parentComponent == null ? null : SwingUtilities.windowForComponent(parentComponent),
      Main.getApplicationFrame()),
      approveButtonText) == JFileChooser.APPROVE_OPTION
  ) {
    result = cacheSaveFileThroughDialog.put(id, fileChooser.getSelectedFile());
  }

  return result;
}
 
Example 17
Source File: PackageSelectorGUI.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Opens dialog window to select root directory of desired installed component package.
 * 
 * @param componentId
 *          The given component ID.
 * @return Selected package root directory or <code>null</code>, if the selection cancelled.
 */
public synchronized File selectPackageDirectory(String componentId) {
  // get last package dir path
  Preferences userPrefs = Preferences.userNodeForPackage(getClass());
  String lastDirPath = (userPrefs != null) ? userPrefs.get(LAST_PACKAGE_DIR_KEY, "./") : "./";
  File lastDir = new File(lastDirPath);
  // create JFileChooser
  JFileChooser fileChooser = new JFileChooser();
  fileChooser.addChoosableFileFilter(new PackageDirFilter());
  fileChooser.setCurrentDirectory(lastDir);
  fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  fileChooser.setDialogTitle("Select " + componentId + " root directory");
  // show information dialog
  String message = "Select the root directory of installed\n" + "\n" + componentId + "\n"
          + "\ncomponent using the following file dialog,\n"
          + "or press 'Cancel' in the file dialog, if this\n"
          + "component is not installed in your file system.";
  JOptionPane.showMessageDialog(_dialogFrame, message);
  // open dialog window
  File selectedDir = null;
  int result = fileChooser.showDialog(_dialogFrame, "Select");
  if (result == JFileChooser.APPROVE_OPTION) {
    // set 'selected dir'
    selectedDir = fileChooser.getSelectedFile();
    if (selectedDir != null) {
      // set 'last dir' in user prefs
      lastDir = selectedDir.getParentFile();
      userPrefs.put(LAST_PACKAGE_DIR_KEY, lastDir.getAbsolutePath());
    }
  }
  return selectedDir;
}
 
Example 18
Source File: FileChooserFactory.java    From keystore-explorer with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Get a JFileChooser filtered for PVK files.
 *
 * @return JFileChooser object
 */
public static JFileChooser getPvkFileChooser() {
	JFileChooser chooser = getFileChooser();
	chooser.addChoosableFileFilter(new FileExtFilter(new String[] { PVK_EXT }, PVK_FILE_DESC));
	return chooser;
}
 
Example 19
Source File: FileChooserFactory.java    From keystore-explorer with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Get a JFileChooser filtered for X.509 Certificate files.
 *
 * @return JFileChooser object
 */
public static JFileChooser getX509FileChooser() {
	JFileChooser chooser = getFileChooser();
	chooser.addChoosableFileFilter(new FileExtFilter(new String[] { X509_EXT_1, X509_EXT_2 }, X509_FILE_DESC));
	return chooser;
}
 
Example 20
Source File: FileChooserFactory.java    From keystore-explorer with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Get a JFileChooser filtered for PKI Path Certificate files.
 *
 * @return JFileChooser object
 */
public static JFileChooser getPkiPathFileChooser() {
	JFileChooser chooser = getFileChooser();
	chooser.addChoosableFileFilter(new FileExtFilter(new String[] { PKI_PATH_EXT }, PKI_PATH_FILE_DESC));
	return chooser;
}