Loading...
Logo
Processing Forum
Hi,

In 1.6, 
Copy code
  1. String selectedFolder = selectFolder(String); 
  2. String selectedInput = selectInput(String);
would prompt the user to select the folder / file with the output being the folder path or file path respectively.
(e.g. Users/adilapapaya/Desktop/myFolder);

In 2.0, selectFolder(String) and selectInput(String) no longer exists. Instead, the closest is requires a callback function which you have to add to your sketch telling it what to do with the selected folder or file. The javadoc/reference gives a nice example of this but I was wondering if it would be possible to keep the previous version of 'selectFolder(String)' and selectInput(String) in there too in v2.0.x.

Not sure if this is helpful, but here's the source code for the previous versions of selectFolder and selectInput. (Both refer to a bunch of other functions though and I haven't checked if those still exist in 2.0).

selectFolder:

Copy code
  1. /*
  2.    * Opens a platform-specific file chooser dialog to select a folder for
  3.    * input. This function returns the full path to the selected folder as a
  4.    * <b>String</b>, or <b>null</b> if no selection.
  5.    */
  6.   public String selectFolder(final String prompt) {
  7.     checkParentFrame();

  8.     try {
  9.       SwingUtilities.invokeAndWait(new Runnable() {
  10.         public void run() {
  11.           if (platform == MACOSX) {
  12.             FileDialog fileDialog =
  13.               new FileDialog(parentFrame, prompt, FileDialog.LOAD);
  14.             System.setProperty("apple.awt.fileDialogForDirectories", "true");
  15.             fileDialog.setVisible(true);
  16.             System.setProperty("apple.awt.fileDialogForDirectories", "false");
  17.             String filename = fileDialog.getFile();
  18.             selectedFile = (filename == null) ? null :
  19.               new File(fileDialog.getDirectory(), fileDialog.getFile());
  20.           } else {
  21.             JFileChooser fileChooser = new JFileChooser();
  22.             fileChooser.setDialogTitle(prompt);
  23.             fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

  24.             int returned = fileChooser.showOpenDialog(parentFrame);
  25.             System.out.println(returned);
  26.             if (returned == JFileChooser.CANCEL_OPTION) {
  27.               selectedFile = null;
  28.             } else {
  29.               selectedFile = fileChooser.getSelectedFile();
  30.             }
  31.           }
  32.         }
  33.       });
  34.       return (selectedFile == null) ? null : selectedFile.getAbsolutePath();

  35.     } catch (Exception e) {
  36.       e.printStackTrace();
  37.       return null;
  38.     }
  39.   }

             

selectInput:  

Copy code
  1. /** Open a platform-specific file chooser dialog to select a file for input.
  2.    * @return full path to the selected file, or null if no selection.*/  
  3. public String selectInput(String prompt) {
  4.       return selectFileImpl(prompt, FileDialog.LOAD);
  5. }
Many thanks,
adila

Replies(7)

I assume the method was updated (and indeed changed to something a little less intuitive) to make it more cross-compatible with javascript and android modes.
There is no Processing 1.6...
And, well, I am not sure what is your question exactly.
If it is a request to have the old behavior, well you shown the code to use for this, so where is the problem?
(I doubt the Processing authors will go back on this one...)
Thanks for the replies.

Sorry, PhiLho, I meant 1.5.1, not 1.6.

I guess my question was if there was a possibility for them to include the 1.5.1 version of selectFolder()/selectInput() in addition to the new version. (Or, is there a downside to leaving the older version in?). Reason being that after installing the latest version, none of my previous applets work anymore without 2 additional functions. I've since switched back to 1.5.1 since most of my applets have some form of selectFolder()/Input() and it's easier to stick to the older version than to update everything (can be done...just tedious is all).

Thanks again for the explanations,
adila




From what I understood, the authors took the opportunity of a new major version (from 1 to 2) to break compatibility with the old ones, to start in a new clean state (almost).
Java deprecated some classes and methods, but never removed them, to carefully avoid to break old programs (although there are some subtle behavior differences breaking them, anyway...). The result is a bloated API with often two ways to do stuff, one of them being discouraged in new code... It is nice for enterprises needing to keep their software investment, but heavy for those starting to code in Java.
Processing took another route, breaking old programs to go forward, unencumbered by history...

Keep carefully the 1.5.1 version if you need to run old code.
Got it. Thanks for the explanation - makes more sense now. :)
- adila
You could try saving the following code as "selectCompat.pde" and dropping the file beside any sketch with this problem that you would like to try in 2.0. I believe this should behave the same as the original (along with blocking while the location is being selected) but I haven't tested it thoroughly. It may be the easiest way to get these sketches running as long as they don't have any other incompatibilities.

String selectFolder() {
  return selectFolder("");
}
String selectFolder(String prompt) {
  SelectCompat select = new SelectCompat();
  selectFolder(prompt, "select", null, select);
  while(!select.done) {}
  return select.result;
}
String selectInput() {
  return selectInput("");
}
String selectInput(String prompt) {
  SelectCompat select = new SelectCompat();
  selectInput(prompt, "select", null, select);
  while(!select.done) {}
  return select.result;
}
String selectOutput() {
  return selectOutput("");
}
String selectOutput(String prompt) {
  SelectCompat select = new SelectCompat();
  selectOutput(prompt, "select", null, select);
  while(!select.done) {}
  return select.result;
}

public class SelectCompat {
  String result;
  boolean done;
  
  public void select(File file) {
    result = file!=null?file.toString():result;
    done = true;
  }
  
}
Thanks! That was really helpful :)

I had to modify your code a bit to make it run the same way as 1.5.1's selectFolder/Input/Output. For some reason, it wouldn't get past this portion in any of the functions
                
Copy code
  1. while(!select.done) {}
so I inserted a dummy 'print' function in there and it works.
Copy code
  1. while(!select.done) { print("");}
I also tried just doing something like " while(!select.done) { count++;} " but that didn't work. I'm wondering if it has anything to do with everything being synchronized internally...?

Either way, thanks again! :)
adila