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 & HelpIntegration › tomc filechooser in a class
Page Index Toggle Pages: 1
tomc filechooser in a class (Read 4940 times)
tomc filechooser in a class
May 15th, 2006, 11:09am
 
I've a problem to use tomc file chooser(http://www.processinghacks.com/hacks/filechooser) in a seperat class. The function works fine but when I try to use it in a class the filechooser window is empty and the programm is crashing.

Quote:


import javax.imageio.*;
import javax.swing.*;

class Pic extends PApplet{
 PImage myImg;
 String subFolder;
 PApplet myApplet;
 Pic(PApplet myApplet){
   this.myApplet=myApplet;  
   myImg=loadImg();
   image(myImg,0,0);
 }

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

   // create a file chooser
   final JFileChooser fc = new JFileChooser();
   println(myApplet);
   // in response to a button click:
   int returnVal = fc.showOpenDialog(myApplet);

   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")) {
       // load the image using the given file path
       img = loadImage(file.getPath());
       if (img != null) {
         // size the window and show the image
         //size(img.width,img.height);

       }
     }
     else {
       // just print the contents to the console
       // note: loadStrings can take a Java File Object too
       String lines[] = loadStrings(file);
       for (int i = 0; i < lines.length; i++) {
         println(lines[i]);  
       }
     }
   }  
   else {
     println("Open command cancelled by user.");
   }
   return img;

 }
}
size(500,500);
Pic a=new Pic(this);


Re: tomc filechooser in a class
Reply #1 - May 15th, 2006, 12:10pm
 
Hmm - what are you actually trying to do?  For simple things, classes are overkill and just confuse things.  Why does your Pic class extend PApplet for example?  It seems like a strange way to get things done.

My hack isn't ideal though, as there are threading issues and clashes with what order things run in.

I had some luck with SwingUtilities.invokeLater, there's an example in this thread: http://processing.org/discourse/yabb_beta/YaBB.cgi?board=;action=usersrecentposts;username=TomC but it won't work for your particular example without some thought - you can't call image() until you know the image exists, so you'd need to move drawing into your draw loop.

Also, Ben once mentioned a built-in method for Processing inputFile or something, but I don't know if it's still there, or if it's supported.
Re: tomc filechooser in a class
Reply #2 - May 15th, 2006, 12:40pm
 
I wanna build a class which is load, store and save images. The extend PApplet was an failure.
Re: tomc filechooser in a class
Reply #3 - May 15th, 2006, 1:56pm
 
The problem seems to be in this line:
Quote:
int returnVal = fc.showOpenDialog(this);

When I call this after a the size() function, the empty window apears, otherwise, like your example, it works fine.
Re: tomc filechooser in a class
Reply #4 - Aug 2nd, 2006, 3:36am
 
I have the same problem.

Whenever I do this in a setup() draw() applet, it will fail and an empty window will appear.
Re: tomc filechooser in a class
Reply #5 - Aug 2nd, 2006, 7:19am
 
Drag and Drop Images onto this applet, and it'll automatically load them.

This is something "like" a filechooser.. or at least functionality you should consider adding for your image-app.

Code:

import java.awt.dnd.*;
import java.awt.datatransfer.*;
PImage img;
void setup()
{
size(200,200);
img = new PImage(width,height);
}

void draw()
{
if(img.width != width && img.height != height) resize(img.width,img.height);
image(img,0,0);
}

void resize(int w, int h)
{
size(w,h);
frame.setSize(w,h);
}

DropTarget dt = new DropTarget(this, new DropTargetListener() {
public void dragEnter(DropTargetDragEvent event) {
// debug messages for diagnostics
//System.out.println("dragEnter " + event);
event.acceptDrag(DnDConstants.ACTION_COPY);
}

public void dragExit(DropTargetEvent event) {
//System.out.println("dragExit " + event);
}

public void dragOver(DropTargetDragEvent event) {
//System.out.println("dragOver " + event);
event.acceptDrag(DnDConstants.ACTION_COPY);
}

public void dropActionChanged(DropTargetDragEvent event) {
//System.out.println("dropActionChanged " + event);
}

public void drop(DropTargetDropEvent event) {
//System.out.println("drop " + event);
event.acceptDrop(DnDConstants.ACTION_COPY);

Transferable transferable = event.getTransferable();
DataFlavor flavors[] = transferable.getTransferDataFlavors();
int successful = 0;

for (int i = 0; i < flavors.length; i++) {
try {
Object stuff = transferable.getTransferData(flavors[i]);
if (!(stuff instanceof java.util.List)) continue;
java.util.List list = (java.util.List) stuff;

for (int j = 0; j < list.size(); j++) {
Object item = list.get(j);
if (item instanceof File) {
File file = (File) item;


String filename = file.getPath();
if(filename.lastIndexOf(".jp") != -1){
img = loadImage(filename);
}else{
println("wrong filetype! jpg supported");
}
}
}

}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
);
Re: tomc filechooser in a class
Reply #6 - Aug 2nd, 2006, 8:29am
 
Yeah, that really cool.
Re: tomc filechooser in a class
Reply #7 - Jul 4th, 2007, 7:39am
 
Question - I've been using inputFile() and outputFile() whenever I needed a chooser because they've seemed to work for me, but they're still undocumented and it seems people have been looking for other solutions.  Are there some problems with these functions that I'm not aware of?  Should I not be relying on them for some reason?
Re: tomc filechooser in a class
Reply #8 - Jul 6th, 2007, 3:46pm
 
Okay, kind of answered my own question when I started having thread hanging issues with the inputFile() call in my latest project.  FWIW, I've found that calling inputFile() as follows (schematically, at least):

import javax.swing.*;
File file;

 SwingUtilities.invokeLater(new Runnable() {
   public void run() {
     file = inputFile();
   }
 });

(you might want to try/catch that call, too) instead of by itself seems to resolve most of these issues, though I'm not entirely sure why.  Anyone care to enlighten?  UI thread-fu is not exactly my specialty (avoiding AWT and Swing and all the associated headaches is one of the best things about Processing)...what exactly is the magic behind invokeLater?
Re: tomc filechooser in a class
Reply #9 - May 21st, 2008, 1:43pm
 
this is precisely what solved my problem. I had problems with JFileChooser, which would randomly hang while I was trying to open the chooser.

Apparently, the invokeLater() method prevents any Swing component from interfering with Swing inner thread.

gah. I spend hours on this. Hope this helps others :)

Code:

import javax.swing.*;
import javax.swing.JFileChooser;


File defaultDirectory = new File(""); // Set Default Directo
JFileChooser fc = new JFileChooser( defaultDirectory );
File[] folderFile = new File[0];


void setup() {
size(200, 200);
background(0);

}

void draw() {}

void mousePressed()
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(null);

if(returnVal == JFileChooser.APPROVE_OPTION)
{
defaultDirectory = chooser.getSelectedFile();
folderFile = defaultDirectory.listFiles();
}
}
catch (Exception e)

{

e.printStackTrace();
}
}
});

}
Re: tomc filechooser in a class
Reply #10 - Dec 24th, 2008, 5:29pm
 
Thanks a lot both of you. I wish I knew aswell what invokelater really does. Just tried awt FileDialog and JFilechooser to have both block. Your solution solved it
Re: tomc filechooser in a class
Reply #11 - Jan 2nd, 2009, 2:58am
 
I had a problem with seltar's code, it seems that now "resize" is a defined method, so it shouldn't name a void.

I've changed the name of that void and everything works fine.
Page Index Toggle Pages: 1