Hi, I would like to implement the SelectFolder()
, SelectInput()
, and SelectOutput()
methods but also specify the folder that they open to.
I’ve spent a lot of time googling this and trying to understand it, but haven’t found a solution. I saw a GitHub discussion from 11 years ago about this, but it seems nothing was done to implement any of it.
It’s important for ease of use that the dialogs open to a specific directory.
Is there any way to do this?
Also, is there any way to filter the SelectInput()
& SelectFolder()
methods to only show files of a certain type, say, of text files?
Thank you,
Mike
1 Like
glv
August 23, 2024, 12:00am
2
Hell @Thundercat ,
This will open to a specified directory:
// Code from these links was modified:
// https://forum.processing.org/beta/num_1140487204.html
// https://stackoverflow.com/questions/13516829/jfilechooser-change-default-directory-in-windows
import javax.swing.*;
PImage img;
boolean loaded = false;
void setup()
{
size(500, 500);
}
void draw()
{
if(loaded)
{
image(img, 0, 0);
println(img.width);
}
}
void mousePressed()
{
launchChooser();
}
void launchChooser()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
loaded = false;
try
{
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("D:/Users/GLV/Pictures/"));
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
//String name = file.getName().toLowerCase();
//if (name.endsWith(".png") || name.endsWith(".gif"))
// {
img = loadImage(file.getAbsolutePath());
println(file.getAbsolutePath());
loaded = true;
println(loaded, img.width, img.height);
//}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
);
}
//void imageChosen()
// {
// image(img, 0, 0, width, height);
// }
I gleaned some insight from the source code here that helped in my search for a solution:
processing4/core/src/processing/core/PApplet.java at main · benfry/processing4 · GitHub
:)
1 Like
Hi @glv , amazing!
I totally appreciate you taking the time to share this!
It works!
I also modified it a little bit so it will work to select a directory instead of just a folder:
// Code from these links was modified:
//https://forum.processing.org/beta/num_1140487204.html
//https://stackoverflow.com/questions/13516829/jfilechooser-change-default-directory-in-windows
import javax.swing.*;
PImage img;
boolean loaded = false;
void setup()
{
size(500, 500);
}
void draw()
{
if(loaded)
{
image(img, 0, 0);
println(img.width);
}
}
void mousePressed()
{
launchChooser();
}
void launchChooser()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
loaded = false;
try
{
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setCurrentDirectory(new File("/Users/Username/Documents/SomeFolder"));
fc.setDialogTitle("Select a folder");
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
//String name = file.getName().toLowerCase();
//if (name.endsWith(".png") || name.endsWith(".gif"))
// {
//img = loadImage(file.getAbsolutePath());
println(file.getAbsolutePath());
//loaded = true;
//println(loaded, img.width, img.height);
//}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
);
}
What about selectOutput() command to select a file?
I’m wondering why processing hasn’t done anything in 11 years with this - it seems like an important thing to have as part of the language!
I totally appreciate your time, so again, THANK YOU for your expertise!!!
Mike
1 Like
glv
August 23, 2024, 8:22am
4
You are welcome!
You get good at finding solutions with experience, patience and persistence.
And I do enjoy the challenges!
Java is new to me (was?) and it can be a challenge at times.
See also:
Hi I know this is an old thread, but how did you do this? I was about to post this exact question. I looked at the references you shared but cannot see how to accomplish it…
Thanks,
Mike
:)
1 Like
You make this forum a better place.
Thank you SO MUCH!
Mike
PS I didn’t know which post to mark as the solution - the link to your other solution is great too! And I like it better because it’s a bit simpler.
thank you again!!
Mike