java - Set a new JFileChooser FileFilter and reset previous ones -
java - Set a new JFileChooser FileFilter and reset previous ones -
i want file filter 2 options, 1 show files , other show specific extension.
the specific extension should 1 selected default.
i'm using same jfilechooser twice, first time show .fas files, , sec show .xls files.
right i'm using code, reasons not overwrite previous file extensions. many answers here on have similar code , same problem, if recycle jfilechooser.
first portion, works
filechooser.setfilefilter(new javax.swing.filechooser.filefilter() { @override public boolean accept(file file) { homecoming file.isdirectory() || file.getabsolutepath().endswith(".fas"); } @override public string getdescription() { homecoming "fasta"; } }); // more modern api, same result // filechooser.setfilefilter(new filenameextensionfilter("file fasta", "fas"));
second portion. used in sec "opening" of same jfilechooser. seems adds filter (the alternative select .fas files remains).
// delete name of selected file file currentdirectory = filechooser.getcurrentdirectory(); filechooser.setselectedfile(new file("")); filechooser.setcurrentdirectory(currentdirectory); // set new file filter filechooser.setfilefilter(new javax.swing.filechooser.filefilter() { @override public boolean accept(file file) { homecoming file.isdirectory() || file.getabsolutepath().endswith(".xls"); } @override public string getdescription() { homecoming "excel 97"; } }); // more modern api, same result // filechooser.setfilefilter(new filenameextensionfilter("excel 97", "xls"));
i guess name setfilefilter
can misleading, because in reality set selected file filter (without replacing other filters).
if want recycle jfilechooser
, simplest solution create phone call resetchoosablefilefilters()
before setting new filters.
code first selection
filechooser.setfilefilter(new filenameextensionfilter("file fasta", "fas"));
code sec selection
// delete name of selected file, remain in same directory file currentdirectory = filechooser.getcurrentdirectory(); filechooser.setselectedfile(new file("")); filechooser.setcurrentdirectory(currentdirectory); // reset current file filters filechooser.resetchoosablefilefilters(); // set new file filter filechooser.setfilefilter(new filenameextensionfilter("excel 97", "xls")); // "all files" filter nowadays too, unless uncomment // filechooser.setacceptallfilefilterused(false);
java jfilechooser filefilter
Comments
Post a Comment