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.
Page Index Toggle Pages: 1
Browse (Read 2296 times)
Browse
Mar 30th, 2010, 2:05pm
 
Is there something in the code that makes the browse window appear? I made an image viewer, and I want it to ask the user for the image.
Re: Browse
Reply #1 - Mar 30th, 2010, 2:11pm
 
PImage theimage;
String selected = selectInput(); //this opens the "browse window"
if(selected!=null)  theimage = loadImage(selected);
Worked!
Reply #2 - Mar 31st, 2010, 6:00am
 
Worked; thanks Wink
Re: Browse
Reply #3 - Apr 1st, 2010, 9:09am
 
One more question:
How can I limit the choice to images only?
Re: Browse
Reply #4 - Apr 1st, 2010, 9:31am
 
You cannot with selectInput.

You would have to copy the code in PApplet.java (selectFileImpl() method) and to add a file filter class.
Re: Browse
Reply #5 - Apr 1st, 2010, 9:45am
 
Can you give an example?
Re: Browse
Reply #6 - Apr 1st, 2010, 10:11am
 
Re: Browse
Reply #7 - Apr 1st, 2010, 12:42pm
 
In theory, it should be:
Code:
import java.awt.FileDialog;
import javax.swing.SwingUtilities;

void setup()
{
size(500, 500);
String filePath = selectImage("Choose an Image");
println(filePath);
}

protected String selectImage(final String prompt)
{
checkParentFrame();

try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
FileDialog fileDialog = new FileDialog(parentFrame, prompt, FileDialog.LOAD);
fileDialog.setFilenameFilter(new FilterImages());
fileDialog.setVisible(true);
String directory = fileDialog.getDirectory();
String filename = fileDialog.getFile();
selectedFile = (filename == null) ? null : new File(directory, filename);
}
});
return (selectedFile == null) ? null : selectedFile.getAbsolutePath();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}

class FilterImages implements FilenameFilter
{
boolean accept(File dir, String name)
{
return name.endsWith(".jpg") || name.endsWith(".png");
}
}

but "Filename filters do not function in Sun's reference implementation for Microsoft Windows."
Because that's AWT, ie. we are using the native Windows file chooser, so we cannot plug in Java code in it.
I will try with the Swing JFileChooser.
Re: Browse
Reply #8 - Apr 1st, 2010, 1:08pm
 
Code:
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.SwingUtilities;

void setup()
{
size(500, 500);
String filePath = selectImage("Choose an Image");
println(filePath);
if (filePath != null)
{
PImage img = loadImage(filePath);
image(img, 0, 0);
}
else
{
exit();
}
}

protected String selectImage(final String prompt)
{
checkParentFrame();

try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle(prompt);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & PNG Images", "jpg", "png");
fileDialog.setFileFilter(filter);
int returnVal = fileDialog.showOpenDialog(parentFrame);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
selectedFile = fileDialog.getSelectedFile();
}
}
});
return (selectedFile == null) ? null : selectedFile.getAbsolutePath();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
Re: Browse
Reply #9 - Apr 2nd, 2010, 12:00pm
 
It worked, but how can I set it to show only images? I don't want it to show anything but folders and images?
Re: Browse
Reply #10 - Apr 2nd, 2010, 12:26pm
 
Are you talking about the first or the second script? The first one indeed doesn't filter out non-image files, as I wrote. The second one is supposed to work...
Page Index Toggle Pages: 1