English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I want coding to filter files of type jpg,bmp and gif in JFileChooser .Please help me with coding.
Thanks in advance!

2006-10-30 13:17:03 · 1 answers · asked by gita k 1 in Computers & Internet Programming & Design

1 answers

That's actually easy. JFileChooser has a FileFilter api for adding file filters. First, create a class that extends javax.swing.filechooser.FileFilter and override the accept() and getDescription() methods. The accept() method contains your logic for what you decide is "acceptable" as a file, while the getDescription() returns a string description to display with your filter in the JFileChooser filter dropdown.

class ImageFileFilter extends FileFilter {

public boolean accept(File f) {
boolean accepted = false;
if (f.isDirectory()){
accepted = true;//allow drill-down into directories
} else {
String file = f.toString();
accepted = file.endsWith(".jpg") || file.endsWith(".bmp) || file.endsWith(".gif");
}
return accepted;
}

public String getDescription() {
String desc = "Image files only (*.jpg, *.bmp, *.gif)";
return desc;
}

}

Then set or add to your JFileChooser's filters:
setFileFilter(new ImageFileFilter()); //if want ONLY this filter
addChoosableFileFilter(new ImageFileFilter());//if want to ADD to dropdown list of filters

2006-10-30 18:41:07 · answer #1 · answered by vincentgl 5 · 0 0

fedest.com, questions and answers