We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › File dialog/selector box
Page Index Toggle Pages: 1
File dialog/selector box? (Read 2659 times)
File dialog/selector box?
May 16th, 2008, 11:21pm
 
Hello,

Is there a way to open a file dialog box in processing for choosing files to open or save?

Alternatively is there a way to check if a file exists (or is located in the data folder) so that the program does not crash if the user tries to load a non-existent file?

Thank you!

Michael
Re: File dialog/selector box?
Reply #1 - May 17th, 2008, 3:18pm
 
The program won't crash if you use a try / catch exception handling around the code accessing the file. Unless it is already done internally by Processing (likely).

Check existence:
Code:
String path = "some path depending on system";
File f = new File(path);
if (f.exists()) print("I found the file.");
(from memory, check Sun docs if it fails.)

Choosing a file: you have to use JFileChooser
Example of Processing code (tested) adapted from the example in the above page:
Code:
import javax.swing.JFileChooser;


JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(chooser.getAcceptAllFileFilter());
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
println("You chose to open this file: " + chooser.getSelectedFile().getName());
}
Re: File dialog/selector box?
Reply #2 - May 18th, 2008, 10:51pm
 
Thank you Philho! Works perfectly.
Re: File dialog/selector box?
Reply #3 - May 31st, 2008, 11:14pm
 
For the first example, to check the file's existence, the default path seems to be (on a mac): /Applications/Processing 0135/

How can I set the default path to be the program's sketch folder instead?

Thanks!
Re: File dialog/selector box?
Reply #4 - Jun 1st, 2008, 10:34am
 
The answer is in the manual page of JFileChooser linked above...
Look at setCurrentDirectory
Re: File dialog/selector box?
Reply #5 - Jun 4th, 2008, 5:36pm
 
Thanks PhiLho,

To be more specific: what I need to do is to get the applet's (or the sketch's) current location - without specifying it through the file chooser.
There must be some function or method that returns the applet's absolute path.

Any ideas?
Thanks
Re: File dialog/selector box?
Reply #6 - Jun 4th, 2008, 9:40pm
 
Code:
println(dataPath("")); 



will return the absolute path of the sketch's data folder.
Re: File dialog/selector box?
Reply #7 - Jun 4th, 2008, 11:16pm
 
probably safer, since dataPath may point inside a jar file:

Code:
File dataDir = new File(sketchPath, "data");
chooser.setSelectedFile(dataDir);


or further, if you are dealing with a jar file, a "data" folder may not actually exist, in which case, you can create one:

Code:
File dataDir = new File(sketchPath, "data");
if (!dataDir.exists()) {
dataDir.mkdirs();
}
chooser.setSelectedFile(dataDir);

Re: File dialog/selector box?
Reply #8 - Aug 11th, 2009, 8:34pm
 
I tried this, but when I put it in, say, my keyPressed handler, the app freezes behind (not a big deal, I could understand if I'm out of the loop) but also the dialog shows up blank, and doesn't respond to being closed, and I have to stop the program in the IDE...
Re: File dialog/selector box?
Reply #9 - Aug 11th, 2009, 8:42pm
 
http://processing.org/discourse/yabb2/num_1140487204_seems_to_have_a_workable_solution.html Smiley
Page Index Toggle Pages: 1