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:net.fabricmc.installer.installer.LocalVersionInstaller.java

public static void install(File mcDir, IInstallerProgress progress) throws Exception {
    JFileChooser fc = new JFileChooser();
    fc.setDialogTitle(Translator.getString("install.client.selectCustomJar"));
    fc.setFileFilter(new FileNameExtensionFilter("Jar Files", "jar"));
    if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        File inputFile = fc.getSelectedFile();

        JarFile jarFile = new JarFile(inputFile);
        Attributes attributes = jarFile.getManifest().getMainAttributes();
        String mcVersion = attributes.getValue("MinecraftVersion");
        Optional<String> stringOptional = ClientInstaller.isValidInstallLocation(mcDir, mcVersion);
        jarFile.close();/*w ww  .  j  ava 2s . c om*/
        if (stringOptional.isPresent()) {
            throw new Exception(stringOptional.get());
        }
        ClientInstaller.install(mcDir, mcVersion, progress, inputFile);
    } else {
        throw new Exception("Failed to find jar");
    }

}

From source file:net.fabricmc.installer.installer.LocalVersionInstaller.java

public static void installServer(File mcDir, IInstallerProgress progress) throws Exception {
    JFileChooser fc = new JFileChooser();
    fc.setDialogTitle(Translator.getString("install.client.selectCustomJar"));
    fc.setFileFilter(new FileNameExtensionFilter("Jar Files", "jar"));
    if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        File inputFile = fc.getSelectedFile();

        JarFile jarFile = new JarFile(inputFile);
        Attributes attributes = jarFile.getManifest().getMainAttributes();
        String fabricVersion = attributes.getValue("FabricVersion");
        jarFile.close();//from   www  .  j  a v  a 2 s .com
        File fabricJar = new File(mcDir, "fabric-" + fabricVersion + ".jar");
        if (fabricJar.exists()) {
            fabricJar.delete();
        }
        FileUtils.copyFile(inputFile, fabricJar);
        ServerInstaller.install(mcDir, fabricVersion, progress, fabricJar);
    } else {
        throw new Exception("Failed to find jar");
    }

}

From source file:br.univali.ps.fuzzy.portugolFuzzyCorretor.control.FileController.java

public static String getCodigoPortugol() throws FileNotFoundException {
    JFileChooser fc = new JFileChooser(currentDirectory);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Portugol Code", "por", "txt");
    fc.setFileFilter(filter);
    fc.setAcceptAllFileFilterUsed(false);
    File file = null;//from   w  w  w  . j  a v a  2  s.  c  om
    int returnVal = fc.showOpenDialog(null);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        file = fc.getSelectedFile();
        currentDirectory = file.getParentFile().getPath();
        return formatarArquivoTexto(file);
    }
    return "";
}

From source file:Main.java

private static JFileChooser getXMLfileChooser() {
    JFileChooser fc = new JFileChooser(new File("../comportamientos"));

    // set ExtensionFile...
    fc.setMultiSelectionEnabled(false);//  w w  w .  j  a v  a 2s  . c om
    fc.setAcceptAllFileFilterUsed(false);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("XML", "xml");
    fc.setFileFilter(filter);

    return fc;
}

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);

    return fileChooser;
}

From source file:kindleclippings.quizlet.QuizletSync.java

private static Map<String, List<Clipping>> readClippingsFile() throws IOException {
    // try to find it
    File cl = new File("/Volumes/Kindle/documents/My Clippings.txt");
    if (!cl.canRead()) {
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FileNameExtensionFilter("Kindle Clippings", "txt"));
        int result = fc.showOpenDialog(null);
        if (result != JFileChooser.APPROVE_OPTION) {
            return null;
        }// ww w . j  a va2  s  .  c om
        cl = fc.getSelectedFile();
    }
    Reader f = new InputStreamReader(new FileInputStream(cl), "UTF-8");
    try {
        MyClippingsReader r = new MyClippingsReader(f);

        Map<String, List<Clipping>> books = new TreeMap<String, List<Clipping>>();

        Clipping l;
        while ((l = r.readClipping()) != null) {
            if (l.getType() != ClippingType.highlight && l.getType() != ClippingType.note) {
                System.err.println("ignored " + l.getType() + " [" + l.getBook() + "]");
                continue;
            }
            String lct = l.getContent().trim();
            if (lct.length() == 0) {
                System.err.println("ignored empty " + l.getType() + " [" + l.getBook() + "]");
                continue;
            }
            if (lct.length() < 10 || !lct.contains(" ")) {
                System.err.println(
                        "ignored too short " + l.getType() + " " + l.getContent() + " [" + l.getBook() + "]");
                continue;
            }
            List<Clipping> clippings = books.get(l.getBook());
            if (clippings == null) {
                clippings = new ArrayList<Clipping>();
                books.put(l.getBook(), clippings);
            }
            clippings.add(l);
        }
        return books;
    } finally {
        f.close();
    }
}

From source file:com.sciaps.utils.Util.java

public static ArrayList<SpectrumShotItem> readCSVFile() {
    ArrayList<SpectrumShotItem> shotItems = new ArrayList<SpectrumShotItem>();

    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(new FileNameExtensionFilter("CSV files", "csv", "CSV"));

    int retrival = chooser.showOpenDialog(null);
    if (retrival == JFileChooser.APPROVE_OPTION) {

        ProgressStatusPanel progressbar = new ProgressStatusPanel();
        final CustomDialog progressDialog = new CustomDialog(Constants.MAIN_FRAME, "Importing CSV file",
                progressbar, CustomDialog.NONE_OPTION);
        progressDialog.setSize(400, 100);
        SwingUtilities.invokeLater(new Runnable() {

            @Override// w  w w.  j a  v a  2 s .co  m
            public void run() {
                progressDialog.setVisible(true);
            }
        });

        try {
            FileReader fr = new FileReader(chooser.getSelectedFile());
            BufferedReader br = new BufferedReader(fr);
            String line;

            while ((line = br.readLine()) != null) {
                shotItems.add(packSpectrumShotItem(line));
            }
            br.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Exception: " + ex.getMessage());
        }

        progressDialog.dispose();
    }

    return shotItems;
}

From source file:Main.java

public static String browseForFile(Window owner, String file, int selectionMode, String title,
        FileFilter filter) {/*from   ww w  . ja  v  a2  s  .c  o m*/
    final String curDir = System.getProperty("user.dir");
    JFileChooser chooser = new JFileChooser(curDir);
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    chooser.setFileSelectionMode(selectionMode);
    chooser.setApproveButtonText("Select");
    chooser.setApproveButtonMnemonic('s');
    chooser.setDialogTitle(title);
    if (filter != null)
        chooser.setFileFilter(filter);

    if (file != null && !file.isEmpty()) {
        File curFile = new File(file);

        chooser.setCurrentDirectory(curFile.getAbsoluteFile().getParentFile());

        if (curFile.isDirectory()) {
            try {
                chooser.setSelectedFile(curFile.getCanonicalFile());
            } catch (IOException ex) {
            }
        } else {
            chooser.setSelectedFile(curFile);
        }
    }

    if (chooser.showOpenDialog(owner) == JFileChooser.APPROVE_OPTION) {
        String path = chooser.getSelectedFile().getPath();
        try {
            path = new File(path).getCanonicalPath();
        } catch (IOException e) {
        }
        // make path relative if possible
        if (path.startsWith(curDir)) {
            path = "." + path.substring(curDir.length());
        }

        return path;
    }
    return null;
}

From source file:ch.admin.hermes.etl.load.HermesETLApplication.java

/**
 * FileOpen Dialog//w  w  w .  j  a va  2  s .c  o  m
 * @param title
 * @param name
 * @param ext
 * @return
 */
private static String getFile(final String title, final String[] name, final String[] ext) {
    // User Chooses a Path and Name for the html Output File
    JFileChooser fc = new JFileChooser(".");
    fc.setDialogType(JFileChooser.OPEN_DIALOG);
    fc.setDialogTitle(title);
    fc.setFileFilter(new FileFilter() {
        public String getDescription() {
            StringBuffer str = new StringBuffer();
            for (String n : name)
                str.append(n + "");
            return (str.toString());
        }

        public boolean accept(File f) {
            for (String e : ext)
                if (f.isDirectory() || f.getName().toLowerCase().endsWith(e))
                    return (true);
            return false;
        }
    });

    int state = fc.showOpenDialog(null);

    if (state == JFileChooser.APPROVE_OPTION)
        return (fc.getSelectedFile().getPath());

    return (null);
}

From source file:Main.java

/**
 * Consistent way to chosing a file to open with JFileChooser.
 * <p>//from  w  w w.j  av a 2s  .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;
}