JFileChooser Help
I am attempting to make a program, and it involves a JFileChooser. I am trying to make it so that the users can only pick .zip files. My code is
开发者_Python百科JFileChooser finder = new JFileChooser();
finder.setFileFilter(new FileNameExtensionFilter(null, ".zip"));
This seems to me like it would run fine, however when I go to a folder with a .zip file, the .zip files are gray, and I can't choose them. How do I fix this? Also, as a side question, how do I get rid of the "All Files" option in the JFileChooser window?
Yes just replace ".zip" with "zip" and also you can remove "All Files" option and make it as "Zip Files". Use following code for that...
JFileChooser fileChooser = new JFileChooser();
// select only zip files and add "Zip Files" option
fileChooser.setFileFilter(new FileNameExtensionFilter("Zip Files", "zip"));
// remove "All Files" option
fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
Use "zip"
as an extension filter, not ".zip"
.
The extension for the FileFilter
should not contain the dot. The dot is the separator between the name and extention parts and not part of the extention. Try it with just zip
not .zip
. See the javadoc for FileFilter for more.
finder.setFileFilter(new FileNameExtensionFilter(null, "zip"));
精彩评论