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 browser window
Page Index Toggle Pages: 1
file browser window (Read 3538 times)
file browser window
Feb 16th, 2006, 5:24pm
 
I keep writing applications that process some file here or there on the computer. I would be nice if I could add a little functionality to my applications and put a file browser in these apps so I can launch one and use it off the cuff, rather than hacking the code every time I want to save or load a different file name.

I've done a quick google and it mentions "swing", which I imagine is a bunch of GUI gubbinz. However, being a bit Java ignorant (see Toxi's rant) I'm unsure how to implement such stuff.

Would someone be able to show me a basic implementation that I can start working with?
Re: file browser window
Reply #1 - Feb 16th, 2006, 6:48pm
 
If you want to dive in head first, the Sun article on JFileChooser should help:

http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html

Swing is the pure Java user interface library.  In Swing, Java draws everything itself but it can be made to look like the file choosers on your platform with a couple of simple tweaks.  You might also try AWT's FileChooser.  AWT was Sun's first attempt at a cross-platform GUI and in some cases looks better because it calls the native OS's graphical widgets.

Re: file browser window
Reply #2 - Feb 16th, 2006, 7:00pm
 
OK here's an example with Swing which loads a .jpg image or prints to the console.  Other things should be reasonably trivial from here.  Obviously this won't work in applets because of security restrictions.

Code:


import javax.swing.*;

// set system look and feel
try {
 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
 e.printStackTrace();
}


//Create a file chooser
final JFileChooser fc = new JFileChooser();

//In response to a button click:
int returnVal = fc.showOpenDialog(this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
 File file = fc.getSelectedFile();
 // see if it's an image
 // (better to write a function and check for all supported extensions)
 if (file.getName().endsWith("jpg")) {
   PImage img = loadImage(file.getPath());
   if (img != null) {
     // size the window and show the image
     size(img.width,img.height);
     image(img,0,0);
   }
 }
 else {
   // just print the contents to the console
   String lines[] = loadStrings(file); // loadStrings can take a Java File Object too
   for (int i = 0; i < lines.length; i++) {
     println(lines[i]);
   }
 }
}
else {
 println("Open command cancelled by user.");
}



AWT is a bit harder (have to be careful what order you do things in otherwise things can hang), but the code is a bit shorter.  If I do an AWT FileDialog example, I'll probably add it here: http://www.processinghacks.com/hacks/filechooser
Re: file browser window
Reply #3 - Feb 18th, 2006, 9:07pm
 
fwiw, the awt hanging thing is a bug that showed up in rev 91, it's the same problem that makes sonia hang, and is detailed here:
http://dev.processing.org/bugs/show_bug.cgi?id=46
Re: file browser window
Reply #4 - Feb 20th, 2006, 12:39pm
 
That's very helpful thankyou very much.

ah - did have a small problem - found out we had the PC version on this Mac. Just had a little runaround this morning trying to get Processing authenticated on all these Macs for the Art of Code class.

Thanks again - magic.
Re: file browser window
Reply #5 - Jul 18th, 2007, 2:55am
 
Quote:
Obviously this won't work in applets because of security restrictions.


Could you explain these security restrictions. That means that there is no way to have Swing elements implemented in applets created with processing? Is there an alternative?

Thanks Smiley
Re: file browser window
Reply #6 - Jul 18th, 2007, 7:43am
 
no. it's just that remote applets running in a browser by default can't read your local files ... which makes sense right

i think if you sign your applet it will be able to read the local files and therefore the file chooser should work.

F
Re: file browser window
Reply #7 - Apr 22nd, 2008, 5:36pm
 
Hello, just look at the new integrated "hacks" sections. It's great!
I found your filechooser exemple. Fantastic, works great on mac ( have to test this on my pc).

Only one thing, do you know if it's possible to select multiple files trough the browser window Smiley

Thanks again and the hacks section looks neat.
Re: file browser window
Reply #8 - Oct 22nd, 2008, 8:24pm
 
This hack example is not aparently working when is not called in the setup function, for example let say when a key is pressed the file browser is open.

Any ideas how to solve it.

Little modificated version of: http://processing.org/hacks/doku.php?id=hacks:filechooser
Code:

import javax.swing.*;
PApplet _this ;
void setup()
{
_this = this;
}

void draw()
{
   chooseFile();
}

void chooseFile()
{
// set system look and feel
try {
 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
 e.printStackTrace();  
}

// create a file chooser
final JFileChooser fc = new JFileChooser();

// in response to a button click:
int returnVal = fc.showOpenDialog(_this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
 File file = fc.getSelectedFile();
 // see if it's an image
 // (better to write a function and check for all supported extensions)

   // file path
   println(file.getPath());

}
}

Re: file browser window
Reply #9 - Oct 23rd, 2008, 5:32pm
 
Does this not work for you guys?

Code:

import javax.swing.UIManager;
import javax.swing.JFileChooser;
File myFile;
void setup()
{
size(200,200);
myFile = chooseFile();
}

File chooseFile()
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
JFileChooser fileDialog = new JFileChooser();
int returnVal = fileDialog.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
return fileDialog.getSelectedFile();
}
return null;
}
void draw()
{
println(myFile == null);
}


seltar
Re: file browser window
Reply #10 - Oct 26th, 2008, 4:44pm
 
Yes works, but my concern was that was not working for me when is called out of setup method - for example the mousePress or keyPress.

I find a solution in Treemap-map code from Fry in Data-visualitzation book. This can be called out of setup(), for example in keyPressed or draw anytime. Add his code in a class.

Code:

class FileChooser
{
 

PApplet _this;
String fileStr;

FileChooser(PApplet t)
{
 _this = t;
}

void selectFile() {
 SwingUtilities.invokeLater(new Runnable() {
   public void run() {
     JFileChooser fc = new JFileChooser(".");
     
     fc.setDialogTitle("Choose a file...");

     int returned = fc.showOpenDialog(frame);
     if (returned == JFileChooser.APPROVE_OPTION) {
       File file = fc.getSelectedFile();
       fileStr = (String)file.getName();
       println(fileStr);
       
     }
   }
 });
}

}
Re: file browser window
Reply #11 - Dec 24th, 2008, 12:28pm
 
Hi,

Just found a alternative to fileChooser. It's called FileDialog.

Here is the Doc http://java.sun.com/j2se/1.4.2/docs/api/java/awt/FileDialog.html

And the example that helped me:
http://www.java-forums.org/java-tips/6330-example-file-dialog.html

I found this because I was getting frustrated not to be able to get the real os browser window when using JFileChooser. Even with the systemLookAndFeel. Has here you get the dialog box just like you set it in your os.

So here is the code, I haven't done much as the example is really good ( thanks to this moderator on java-forums). you can give a title to your dialog box window, select default directory to start with, the position of the box and the file type. And it's really easy to use. Smiley

Quote:
import java.awt.*;
import java.awt.event.*;
import java.io.*;

void setup(){
  size(400, 400);
}

void draw(){


}

void keyReleased(){
  if(key == 'o') println( loadFile(new Frame(), "open your favorite file", "/Users/myName/Desktop/", "") );
  if(key == 's') println( saveFile(new Frame(), "save your great work", "", "") );
}



String loadFile (Frame f, String title, String defDir, String fileType) {
  FileDialog fd = new FileDialog(f, title, FileDialog.LOAD);
  fd.setFile(fileType);
  fd.setDirectory(defDir);
  fd.setLocation(50, 50);
  fd.show();
  String path = fd.getDirectory()+fd.getFile();
    return path;
}

String saveFile (Frame f, String title, String defDir, String fileType) {
  FileDialog fd = new FileDialog(f, title,    FileDialog.SAVE);
  fd.setFile(fileType);
  fd.setDirectory(defDir);
  fd.setLocation(50, 50);
  fd.show();
  String path = fd.getDirectory()+fd.getFile();
    return path;
}






Re: file browser window
Reply #12 - Dec 24th, 2008, 12:30pm
 
If people think it's useful, should I had see to Hacks?

MODIFIED:

FileDialog is more restricted then FileChooser. Can't do just directory or just files, can't do multiple files, can't add filters, etc...

But I find that with a eclipse library there might be solutions ( http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/DemonstratesFileDialog.htm ).

I think that filedialog is for basic usage, and filechooser is more custom dialog box, like if you want the box to match your programme's design or have some special features that doesn't exist in the os dialog box.
Page Index Toggle Pages: 1