Blocking version of selectFolder()

As per the subject, is there a way to create a blocking version of selectFolder()? I want to ask for the folder of images in setup(), it doesn't make sense to proceed without that directory.

Answers

  • You mean selectFolder () is blocking the execution of the rest of your sketch until selectFolder has given a result...?

  • @paulbourke -- it would help to know when are you calling this selectFolder() (at the beginning of the sketch, or in the middle) and what specifically are you blocking -- the contents of draw()?

    Here is an example that adds the built-ins loop() and noLoop() to the default reference sketch for selectFolder(). The text shows the current draw frame. Test it by clicking with the mouse.

    boolean select;
    
    void draw() {
      background(0);
      text(frameCount, width/2,height/2);
      if(select){
        selectFolder("Select a folder to process:", "folderSelected");
        select=false;
        noLoop();     // pause drawing
        return;       // end the current loop
      }
    }
    
    void folderSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } else {
        println("User selected " + selection.getAbsolutePath());
      }
      loop(); // start drawing again
    }
    
    void mouseClicked(){
      select=true;
    }
    
  • Answer ✓

    Yeah, thanks. I did something similar without knowing about loop/noloop. Declared a boolean global as false, did nothing in the draw while it was false, set it true in the callback.

  • Nicely done!

    I always integrate this boolean into the state concept I use for bigger projects.

  • The main distinction is that with a boolean, frameCount will continue to iterate as draw() loops at frameRate per sec.

    This only matters you use frameCount-based math for animation -- and then it that may be a good or bad thing depending on what your desired behavior is.

  • The standard Java file chooser dialog window is synchronous which means it halts (blocks) the execution of the main thread until it is closed by choosing an option.

    This was how Processing worked until V2 (~Jan 2013) when the Processing developers changed the file/folder chooser code so it ran in another thread making it asynchronous (non blocking).

    The most obvious way is to ad your own method to use the Java file chooser directly.

    This webpage gives sample code to select a folder. Alternatively use G4P as its dialogs are asynchronous, I added them to provide an alternative to the methods provided by Processing.

Sign In or Register to comment.