JFileChooser: setFileFilter(FileFilter filter) : JFileChooser « javax.swing « Java by API






JFileChooser: setFileFilter(FileFilter filter)

  
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class MainClass {

  public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    FileFilter filter1 = new ExtensionFileFilter("JPG and JPEG", new String[] { "JPG", "JPEG" });
    fileChooser.setFileFilter(filter1);
    int status = fileChooser.showOpenDialog(null);
    if (status == JFileChooser.APPROVE_OPTION) {
      File selectedFile = fileChooser.getSelectedFile();
      System.out.println(selectedFile.getParent());
      System.out.println(selectedFile.getName());
    } else if (status == JFileChooser.CANCEL_OPTION) {
      System.out.println(JFileChooser.CANCEL_OPTION);
    }
  }
}

class ExtensionFileFilter extends FileFilter {
  String description;

  String extensions[];

  public ExtensionFileFilter(String description, String extension) {
    this(description, new String[] { extension });
  }

  public ExtensionFileFilter(String description, String extensions[]) {
    if (description == null) {
      this.description = extensions[0];
    } else {
      this.description = description;
    }
    this.extensions = (String[]) extensions.clone();
    toLower(this.extensions);
  }

  private void toLower(String array[]) {
    for (int i = 0, n = array.length; i < n; i++) {
      array[i] = array[i].toLowerCase();
    }
  }

  public String getDescription() {
    return description;
  }

  public boolean accept(File file) {
    if (file.isDirectory()) {
      return true;
    } else {
      String path = file.getAbsolutePath().toLowerCase();
      for (int i = 0, n = extensions.length; i < n; i++) {
        String extension = extensions[i];
        if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.')) {
          return true;
        }
      }
    }
    return false;
  }
}


           
         
    
  








Related examples in the same category

1.JFileChooser.APPROVE_OPTION
2.JFileChooser.CANCEL_OPTION
3.JFileChooser.DIRECTORIES_ONLY
4.JFileChooser.DIRECTORY_CHANGED_PROPERTY
5.JFileChooser.ERROR_OPTION
6.JFileChooser.SELECTED_FILE_CHANGED_PROPERTY
7.JFileChooser.SELECTED_FILES_CHANGED_PROPERTY
8.JFileChooser: addActionListener(ActionListener l)
9.JFileChooser: addChoosableFileFilter(FileFilter filter)
10.JFileChooser: addPropertyChangeListener(PropertyChangeListener listener)
11.JFileChooser: createDialog(Component parent)
12.JFileChooser: getCurrentDirectory()
13.JFileChooser: getFileSystemView()
14.JFileChooser: getSelectedFile()
15.JFileChooser: getTypeDescription(File f)
16.JFileChooser: isFileHidingEnabled()
17.JFileChooser: setAccessory(JComponent newAccessory)
18.JFileChooser: setApproveButtonMnemonic(char mnemonic)
19.JFileChooser: setApproveButtonText(String approveButtonText)
20.JFileChooser: setApproveButtonToolTipText(String toolTipText)
21.JFileChooser: setControlButtonsAreShown(boolean b)
22.JFileChooser: setCurrentDirectory(File dir)
23.JFileChooser: setFileHidingEnabled(boolean b)
24.JFileChooser: setFileSelectionMode(int mode)
25.JFileChooser: setFileView(FileView fileView)
26.JFileChooser: setMultiSelectionEnabled(boolean b)
27.JFileChooser: setSelectedFile(File file)
28.JFileChooser: showOpenDialog(Component parent)