Example usage for javax.swing JFileChooser setFileFilter

List of usage examples for javax.swing JFileChooser setFileFilter

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "Sets the File Filter used to filter out files of type.")
public void setFileFilter(FileFilter filter) 

Source Link

Document

Sets the current file filter.

Usage

From source file:com.floreantpos.bo.actions.DataExportAction.java

public static JFileChooser getFileChooser() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fileChooser.setMultiSelectionEnabled(false);
    fileChooser.setSelectedFile(new File("floreantpos-menu-items.xml")); //$NON-NLS-1$
    fileChooser.setFileFilter(new FileFilter() {

        @Override// ww w  .  j av a 2 s.c o  m
        public String getDescription() {
            return "XML File"; //$NON-NLS-1$
        }

        @Override
        public boolean accept(File f) {
            if (f.getName().endsWith(".xml")) { //$NON-NLS-1$
                return true;
            }

            return false;
        }
    });
    return fileChooser;
}

From source file:Main.java

/**
 * Consistent way to chosing multiple files to open with JFileChooser.
 * <p>/*from  w w w .jav  a 2  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[] 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:lectorarchivos.VerCSV.java

public static String abrirSelectorXML() throws IOException {
    JFileChooser selector = new JFileChooser();
    FileNameExtensionFilter filtroArchivo = new FileNameExtensionFilter("csv & xls & txt", "csv", "xls", "txt");
    selector.setFileFilter(filtroArchivo);
    selector.setFileSelectionMode(JFileChooser.FILES_ONLY);
    selector.setMultiSelectionEnabled(false);

    //int r=selector.showOpenDialog(this);

    /*if(r==JFileChooser.APPROVE_OPTION){
    //Recorremos el array de ficheros seleccionados
    rutaFichero = selector.getSelectedFile().toPath().toAbsolutePath().toString(); 
    }else{/*  w  ww  .  j a va2s . com*/
    System.out.println("Yo devuelvo nullo");
    }*/
    return rutaFichero;
}

From source file:SwingUtil.java

/**
 * Get a file selection using the FileChooser dialog.
 *
 * @param owner//  www  .j  ava  2  s . com
 * The parent of this modal dialog.
 * @param defaultSelection
 * The default file selection as a file.
 * @param filter
 * An extension filter
 * @param title
 * The caption for the dialog.
 *
 * @return
 * A selected file or null if no selection is made.
 */
public static File getFileChoice(Component owner, File defaultSelection, FileFilter filter, String title) {
    //
    // There is apparently a bug in the native Windows FileSystem class that
    // occurs when you use a file chooser and there is a security manager
    // active. An error dialog is displayed indicating there is no disk in
    // Drive A:. To avoid this, the security manager is temporarily set to
    // null and then reset after the file chooser is closed.
    //
    SecurityManager sm = null;
    File choice = null;
    JFileChooser chooser = null;

    sm = System.getSecurityManager();
    System.setSecurityManager(null);

    chooser = new JFileChooser();
    if (defaultSelection.isDirectory()) {
        chooser.setCurrentDirectory(defaultSelection);
    } else {
        chooser.setSelectedFile(defaultSelection);
    }
    chooser.setFileFilter(filter);
    chooser.setDialogTitle(title);
    chooser.setApproveButtonText("OK");
    int v = chooser.showOpenDialog(owner);

    owner.requestFocus();
    switch (v) {
    case JFileChooser.APPROVE_OPTION:
        if (chooser.getSelectedFile() != null) {
            choice = chooser.getSelectedFile();
        }
        break;
    case JFileChooser.CANCEL_OPTION:
    case JFileChooser.ERROR_OPTION:
    }
    chooser.removeAll();
    chooser = null;
    System.setSecurityManager(sm);
    return choice;
}

From source file:com.igormaznitsa.ideamindmap.utils.IdeaUtils.java

public static File chooseFile(final Component parent, final boolean filesOnly, final String title,
        final File selectedFile, final FileFilter filter) {
    final JFileChooser chooser = new JFileChooser(selectedFile);

    chooser.setApproveButtonText("Select");
    if (filter != null)
        chooser.setFileFilter(filter);

    chooser.setDialogTitle(title);//  w  w  w  .ja  v a2 s . co m
    chooser.setMultiSelectionEnabled(false);

    if (filesOnly)
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    else
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

    if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
        return chooser.getSelectedFile();
    } 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, 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  w w  w . j  a  va 2  s  .  co  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);/*from  w  w  w .ja v a 2  s .  c o m*/
    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);//from w  w  w. j  a va2s.  c om
        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:Main.java

/**
 * Displays a {@link JFileChooser} to select a file.
 *
 * @param title The title of the dialog.
 * @param startDirectory The directory where the dialog is initialed opened.
 * @param fileExtension File extension to filter each content of the opened
 * directories. Example ".xml".// w w w  .  j a v a 2s . c  om
 * @param startFile The preselected file where the dialog is initialed opened.
 * @return The {@link File} object selected, returns null if no file was selected.
 */
public static File chooseFile(String title, String startDirectory, final String fileExtension,
        String startFile) {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setDialogTitle(title);

    if (fileExtension != null && !fileExtension.trim().equals("")) {
        FileFilter filter = new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                return pathname.isDirectory() || pathname.getName().endsWith(fileExtension);
            }

            @Override
            public String getDescription() {
                return "(" + fileExtension + ")";
            }
        };

        chooser.setFileFilter(filter);
    }

    if (startDirectory != null && !startDirectory.trim().equals("")) {
        chooser.setCurrentDirectory(new File(startDirectory));
    }

    if (startFile != null && !startFile.trim().equals("")) {
        chooser.setSelectedFile(new File(startFile));
    }

    int status = chooser.showOpenDialog(null);

    if (status == JFileChooser.APPROVE_OPTION) {
        return chooser.getSelectedFile();
    }

    return null;
}

From source file:at.tuwien.ifs.somtoolbox.apps.viewer.fileutils.ExportUtils.java

private static JFileChooser initFileChooser(JFileChooser fileChooser, FileFilter filter) {
    if (fileChooser.getSelectedFile() != null) { // reusing the dialog
        fileChooser = new JFileChooser(fileChooser.getSelectedFile().getPath());
    }//from  www. j a  v  a2s. c o m
    if (filter != null) {
        fileChooser.setFileFilter(filter);
    }
    return fileChooser;
}