-
JFileChooser restriction
In my Java program, I use a JFileChooser to bring in a file for processing. How can I restrict the JFileChooser to only accept a file of type .txt, or do a System.exit(1) otherwise. Or better yet, can I set it up so that the JFileChooser only displays .txt files in the window.
-
You can set a file filter such as:
Code:
private class TxtFileFilter extends FileFilter{
public boolean accept(File file){
if(file.isDirectory()) return true;
String fname = file.getName();
return fname.endsWith("txt");
}
public String getDescription(){
return "txt file";
}
}
Susanna
-
There's already a class javax.swing.filechooser.FileNameExtensionFilter in the standard JDK. Why reinvent the wheel?
db