Dropdown list as FilenameFilter in selectFolder() Dialog box

Hi guys… been racking my brains over this. I wanted to do a sort of extension filtering much like in the pic here… where the user can select the file extension or type eg: jpg, png, avi, mp4, etc

I went down the route of FilenameFilter() before realizing that it only does that under the hood so to speak.

How may I go about it visually to get it represented like in the picture? Thanks! :slight_smile:

import java.io.File;
import java.io.FilenameFilter;

String selectedAbsolutePath, selectedFolderName;


void setup() {
  size(300, 300);
}

void draw() {
}

void mousePressed() {
  println("Directory selection is pressed");
  OpenDirectory();  
}

void OpenDirectory() {  
  selectFolder("Select a folder to process:", "setDirectoryPath");
}

void setDirectoryPath(File selection) {
  if (selection != null) {
    selectedAbsolutePath = selection.getAbsolutePath();
    selectedFolderName = selection.getName();
    println(selectedAbsolutePath);
    println(selectedFolderName);

    try {
      // Create a file object
      File f = new File(selectedAbsolutePath);
      
      // Create a FilenameFilter
      FilenameFilter filter = new FilenameFilter() {

        boolean accept(File f, String name)
        {
          return name.endsWith(".jpg");
        }
      };

      // Get all the names of the files present
      // in the given directory
      // and whose names end with ".jpg"
      File[] files = f.listFiles(filter);

      println("Files are:");

      // Display the names of the files
      for (int i = 0; i < files.length; i++) {
        println(files[i].getName());
      }
    }
    catch(Exception e) {
      System.err.println(e.getMessage());
    }
  } else {
    println("Windows was or user hit cancel.");
  }
}

Hi guys,

Managed to put together some bit of code that does this but of course it uses Swing and I know I will run into issues later on. But this gives a better idea of what I wanted to do. Perhaps there is a different way to represent this in Processing. Do share your thoughts. Thanks.

import javax.swing.JFileChooser;
import java.io.File;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.util.prefs.Preferences;

String LAST_USED_FOLDER = "";

Preferences prefs = Preferences.userRoot().node(getClass().getName());
JFileChooser fileChooser = new JFileChooser(prefs.get(LAST_USED_FOLDER, new File(".").getAbsolutePath()));

fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Documents", "pdf"));
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("MS Office Documents", "docx", "xlsx", "pptx"));
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp"));

fileChooser.setAcceptAllFileFilterUsed(true);

int result = fileChooser.showOpenDialog(fileChooser);

if (result == JFileChooser.APPROVE_OPTION) {
  prefs.put(LAST_USED_FOLDER, fileChooser.getSelectedFile().getParent());
  File selectedFile = fileChooser.getSelectedFile();
  System.out.println("Selected file: " + selectedFile.getAbsolutePath());