Here you can find the source of setFileChooserFilters(JFileChooser fileDlg, String filters, int selectIndex)
public static void setFileChooserFilters(JFileChooser fileDlg, String filters, int selectIndex)
//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); } } }