Java JFileChooser setFileChooserFilters(JFileChooser fileDlg, String filters, int selectIndex)

Here you can find the source of setFileChooserFilters(JFileChooser fileDlg, String filters, int selectIndex)

Description

set File Chooser Filters

License

Open Source License

Declaration

public static void setFileChooserFilters(JFileChooser fileDlg, String filters, int selectIndex) 

Method Source Code

//package com.java2s;
/*/* w w w .  j  ava2 s.c  o m*/
 *  "BSLib", Brainstorm Library.
 *  Copyright (C) 2017 by Sergey V. Zhdanovskih.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Main {
    public static void setFileChooserFilters(JFileChooser fileDlg, String filters, int selectIndex) {
        if (fileDlg == null) {
            throw new IllegalArgumentException("fileDlg");
        }

        FileFilter selectFilter = null;
        String[] filterParts = filters.split("[|]", -1);
        int filtersNum = filterParts.length / 2;
        for (int i = 0; i < filtersNum; i++) {
            int idx = i * 2;
            String name = filterParts[idx];
            String exts = filterParts[idx + 1];
            String[] extensions = exts.split("[,]", -1);

            String[] newExts = new String[extensions.length];
            for (int k = 0; k < extensions.length; k++) {
                String ext = extensions[k];
                if (ext.startsWith("*.")) {
                    ext = ext.substring(2);
                }
                newExts[k] = ext;
            }

            FileFilter fneFilter = new FileNameExtensionFilter(name, newExts);
            fileDlg.addChoosableFileFilter(fneFilter);
            if (selectIndex > 0 && (i == selectIndex - 1)) {
                selectFilter = fneFilter;
            }
        }

        if (selectFilter != null) {
            fileDlg.setFileFilter(selectFilter);
        }
    }
}

Related

  1. selectFile(String msg, File dir, FileFilter filterOrNull)
  2. selectFileForOpen(final Component parent, final FileFilter[] fileFilters, final String title, final FileFilter[] selectedFilters, final File initFile)
  3. selectNonExistingFile(Component parent, String extensionWanted)
  4. setCurrentDirectory(JFileChooser fileChooser, String location)
  5. setFile(File file, JFileChooser chooser)
  6. setSelectedFile(JFileChooser chooser, File file)
  7. SHOW_JFILECHOOSER(String title, String approvetext, int fileselectionmode, boolean multiselection)
  8. showFileChooserLoop(Component parentComponent, JFileChooser chooser)
  9. write(File file, String text)