Example usage for javax.swing JFileChooser addChoosableFileFilter

List of usage examples for javax.swing JFileChooser addChoosableFileFilter

Introduction

In this page you can find the example usage for javax.swing JFileChooser addChoosableFileFilter.

Prototype

@BeanProperty(preferred = true, description = "Adds a filter to the list of user choosable file filters.")
public void addChoosableFileFilter(FileFilter filter) 

Source Link

Document

Adds a filter to the list of user choosable file filters.

Usage

From source file:ExcelComponents.FileOpener.java

public static File[] openfiles() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.addChoosableFileFilter(excelfilter);
    fileChooser.setMultiSelectionEnabled(true);
    int returnValue = fileChooser.showOpenDialog(null);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File[] selectedFiles = fileChooser.getSelectedFiles().clone();
        return (selectedFiles);
    }/*from w  w  w  . j ava 2s .c o m*/
    return null;

}

From source file:com.willwinder.universalgcodesender.uielements.components.FirmwareSettingsFileTypeFilter.java

public static JFileChooser getSettingsFileChooser() {
    FirmwareSettingsFileTypeFilter filter = new FirmwareSettingsFileTypeFilter();

    // Setup file browser with the last path used.
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fileChooser.setFileHidingEnabled(true);
    fileChooser.addChoosableFileFilter(filter);
    fileChooser.setAcceptAllFileFilterUsed(true);
    fileChooser.setFileFilter(filter);/*w  w w  .  j  av a 2 s.c o m*/

    return fileChooser;
}

From source file:com.mgmtp.perfload.loadprofiles.ui.util.SwingUtils.java

public static JFileChooser createFileChooser(final File dir, final String description, final String extension) {
    JFileChooser fc = new JFileChooser(dir);
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fc.setMultiSelectionEnabled(false);//  w w  w.  j  a  va  2 s  .co  m
    fc.setAcceptAllFileFilterUsed(false);
    fc.addChoosableFileFilter(new FileFilter() {

        @Override
        public String getDescription() {
            return description;
        }

        @Override
        public boolean accept(final File f) {
            return f.isDirectory() || FilenameUtils.isExtension(f.getName(), extension);
        }
    });
    return fc;
}

From source file:Main.java

/**
 * Consistent way to chosing a file to open with JFileChooser.
 * <p>//from  w  ww.  ja v  a2 s.  c  om
 * 
 * @see JFileChooser#setFileSelectionMode(int)
 * @see #getSystemFiles(Component, int)
 * @param owner to show the component relative to.
 * @param mode selection mode for the JFileChooser.
 * @return File based on the user selection can be null.
 */
public static File getSystemFile(Component owner, int mode, FileFilter[] filters) {

    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(mode);
    jfc.setFileHidingEnabled(true);
    jfc.setAcceptAllFileFilterUsed(true);
    if (filters != null) {
        for (int i = 0; i < filters.length; i++) {
            jfc.addChoosableFileFilter(filters[i]);
        }

        if (filters.length >= 1) {
            jfc.setFileFilter(filters[0]);
        }
    }

    int result = jfc.showOpenDialog(owner);

    if (result == JFileChooser.APPROVE_OPTION) {
        return jfc.getSelectedFile();
    }

    return null;
}

From source file:Main.java

/**
 * Consistent way to chosing multiple files to open with JFileChooser.
 * <p>/*from ww  w. j  a va2s  . c  o m*/
 * 
 * @see JFileChooser#setFileSelectionMode(int)
 * @see #getSystemFiles(Component, int)
 * @param owner to show the component relative to.
 * @param mode selection mode for the JFileChooser.
 * @return File[] based on the user selection can be null.
 */
public static File[] getSystemFiles(Component owner, int mode, FileFilter[] filters) {

    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(mode);
    jfc.setFileHidingEnabled(true);
    jfc.setMultiSelectionEnabled(true);
    jfc.setAcceptAllFileFilterUsed(true);
    if (filters != null) {
        for (int i = 0; i < filters.length; i++) {
            jfc.addChoosableFileFilter(filters[i]);
        }

        if (filters.length >= 1) {
            jfc.setFileFilter(filters[0]);
        }
    }

    int result = jfc.showOpenDialog(owner);
    if (result == JFileChooser.APPROVE_OPTION) {
        return jfc.getSelectedFiles();
    }

    return new File[0];
}

From source file:net.menthor.editor.v2.util.Util.java

public static File chooseFile(Component parent, String lastPath, String dialogTitle, String fileDescription,
        String fileExtension, boolean checkOverrideFile) throws IOException {
    JFileChooser fileChooser = createChooser(lastPath, checkOverrideFile);
    fileChooser.setDialogTitle(dialogTitle);
    FileNameExtensionFilter filter = new FileNameExtensionFilter(fileDescription, fileExtension);
    fileChooser.addChoosableFileFilter(filter);
    if (SystemUtil.onWindows())
        fileChooser.setFileFilter(filter);
    fileChooser.setAcceptAllFileFilterUsed(false);
    if (fileChooser.showDialog(parent, "Ok") == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        if (!file.getName().endsWith("." + fileExtension)) {
            file = new File(file.getCanonicalFile() + "." + fileExtension);
        } else {//from  ww w .  j  a v a2  s .  c  o  m
            file = new File(file.getCanonicalFile() + "");
        }
        return file;
    } else {
        return null;
    }
}

From source file:net.menthor.editor.v2.util.Util.java

public static File chooseFile(Component parent, String lastPath, String dialogTitle, String fileDescription,
        String fileExtension, String fileExtension2, boolean checkOverrideFile) throws IOException {
    JFileChooser fileChooser = createChooser(lastPath, checkOverrideFile);
    fileChooser.setDialogTitle(dialogTitle);
    FileNameExtensionFilter filter = new FileNameExtensionFilter(fileDescription, fileExtension,
            fileExtension2);/* www.  j  av a2  s  . c  om*/
    fileChooser.addChoosableFileFilter(filter);
    if (SystemUtil.onWindows())
        fileChooser.setFileFilter(filter);
    fileChooser.setAcceptAllFileFilterUsed(false);
    if (fileChooser.showDialog(parent, "Ok") == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        if (!(file.getName().endsWith("." + fileExtension))
                && !(file.getName().endsWith("." + fileExtension2))) {
            file = new File(file.getCanonicalFile() + "." + fileExtension2);
        } else {
            file = new File(file.getCanonicalFile() + "");
        }
        return file;
    } else {
        return null;
    }
}

From source file:net.sf.jabref.importer.ImportFormats.java

private static JFileChooser createImportFileChooser(String currentDir) {

    SortedSet<ImportFormat> importers = Globals.IMPORT_FORMAT_READER.getImportFormats();

    String lastUsedFormat = Globals.prefs.get(JabRefPreferences.LAST_USED_IMPORT);
    FileFilter defaultFilter = null;
    JFileChooser fc = new JFileChooser(currentDir);
    Set<ImportFileFilter> filters = new TreeSet<>();
    for (ImportFormat format : importers) {
        ImportFileFilter filter = new ImportFileFilter(format);
        filters.add(filter);/* www  . j  a  v  a  2s .c o  m*/
        if (format.getFormatName().equals(lastUsedFormat)) {
            defaultFilter = filter;
        }
    }
    for (ImportFileFilter filter : filters) {
        fc.addChoosableFileFilter(filter);
    }

    if (defaultFilter == null) {
        fc.setFileFilter(fc.getAcceptAllFileFilter());
    } else {
        fc.setFileFilter(defaultFilter);
    }
    return fc;
}

From source file:ec.util.chart.swing.Charts.java

public static void saveChart(@Nonnull ChartPanel chartPanel) throws IOException {
    JFileChooser fileChooser = new JFileChooser();
    FileFilter defaultFilter = new FileNameExtensionFilter("PNG (.png)", "png");
    fileChooser.addChoosableFileFilter(defaultFilter);
    fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("JPG (.jpg) (.jpeg)", "jpg", "jpeg"));
    if (Charts.canWriteChartAsSVG()) {
        fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("SVG (.svg)", "svg"));
        fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Compressed SVG (.svgz)", "svgz"));
    }/*from ww  w. j a  v  a2s. c om*/
    fileChooser.setFileFilter(defaultFilter);
    File currentDir = chartPanel.getDefaultDirectoryForSaveAs();
    if (currentDir != null) {
        fileChooser.setCurrentDirectory(currentDir);
    }
    if (fileChooser.showSaveDialog(chartPanel) == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        try (OutputStream stream = Files.newOutputStream(file.toPath())) {
            writeChart(getMediaType(file), stream, chartPanel.getChart(), chartPanel.getWidth(),
                    chartPanel.getHeight());
        }
        chartPanel.setDefaultDirectoryForSaveAs(fileChooser.getCurrentDirectory());
    }
}

From source file:net.sf.jabref.exporter.ExportFormats.java

private static JFileChooser createExportFileChooser(String currentDir) {
    String lastUsedFormat = Globals.prefs.get(JabRefPreferences.LAST_USED_EXPORT);
    FileFilter defaultFilter = null;
    JFileChooser fc = new JFileChooser(currentDir);
    Set<FileFilter> filters = new TreeSet<>();
    for (Map.Entry<String, IExportFormat> e : ExportFormats.EXPORT_FORMATS.entrySet()) {
        String formatName = e.getKey();
        IExportFormat format = e.getValue();
        filters.add(format.getFileFilter());
        if (formatName.equals(lastUsedFormat)) {
            defaultFilter = format.getFileFilter();
        }//from w  ww.  ja  va 2s .  c om
    }
    for (FileFilter ff : filters) {
        fc.addChoosableFileFilter(ff);
    }
    fc.setAcceptAllFileFilterUsed(false);
    if (defaultFilter != null) {
        fc.setFileFilter(defaultFilter);
    }
    return fc;
}