简体   繁体   中英

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

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

使用"zip"作为扩展过滤器,而不是".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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM