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 & HelpPrograms › Help with file chooser
Page Index Toggle Pages: 1
Help with file chooser (Read 491 times)
Help with file chooser
Apr 7th, 2009, 1:58pm
 
Hi, I'm working on a program that needs a dialog box to pop up so the user can select an audio file whenever they click a certain button. Right now my program either freezes while the dialog box is opening, or immediately after the user has selected a file. Can anyone please help me fix this freezing problem?

The code is as follows

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

pickAudio audioSelect;
JFileChooser chooser;
audioFileFilter audioFilter;

boolean locked;

//Display font
PFont font;

void setup()
{
 size(800, 600);
 locked = false;
 
 audioSelect = new pickAudio(4, 4);
 //set the display font to courier
 font = createFont("Courier New Bold", 20);
 textFont(font);
}

void draw()
{
 background(255);
 audioSelect.display();
}

void mouseReleased()
{
 if(audioSelect.clicked == true)
 {
   audioSelect.initialize();
 }
 
 locked = false;
 println(audioSelect.path);
}

//Make a file filter for the dialog
//that only takes audio files usable by
//the audio library
class audioFileFilter extends FileFilter
{
 //Give it a label
 String getDescription()
 {
   return "Audio Files";
 }
 
 //Shows what kind of files to show
 boolean accept(File f)
 {
   //get the name of the passed file
   String filename = f.getName();
   //try to find the last in the filename period
   //if no period is found, return an empty string
   //otherwise, return whatever is after the period
   String extension = (filename.lastIndexOf(".")==-1)?"":
   filename.substring(filename.lastIndexOf(".")+1,filename.length());
   
   //if the extension falls under certain audio types, return true
   if(extension.equals("mp3") || extension.equals("wav") ||
   extension.equals("aiff") || extension.equals("au") || extension.equals("snd"))
   {
     return true;
   }
   
   //return true for folders as well
   if(f.isDirectory())
   {
     return true;
   }
   
   //if nothing else returned true, return false
   return false
Re: Help with file chooser
Reply #1 - Apr 7th, 2009, 2:57pm
 
I think your code got cut off.

Things that I've gotten snagged by are mouse events while the dialog is up. Make sure that you call noLoop() just before you open the dialog. Then call loop() after.
Re: Help with file chooser
Reply #2 - Apr 7th, 2009, 3:15pm
 
Whoops, here's the rest of my code. I tried the loop/noLoop thing, but no dice so far.

}

class pickAudio
{
 String path;
 float xpos, ypos, btnX, btnY;
 boolean clicked;
 
 pickAudio()
 {
   path = null;
   btnX = 50;
   btnY = 20;
   xpos = width - (btnX + 5);
   ypos = 5;
   clicked = false;
 }
 
 pickAudio(float sliderNum, float sliderTot)
 {
   path = null;
   btnX = 50;
   btnY = 20;
   xpos = width * (sliderNum / (sliderTot + 1)) - (btnX/2);
   ypos = height - btnY * 1.5;
   clicked = false;
 }
 
 void initialize()
 {
   JFileChooser chooser = new JFileChooser();
   audioFileFilter audioFilter = new audioFileFilter();
   chooser.setFileFilter(audioFilter);
   chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter());
   
   int returnVal = chooser.showOpenDialog(null);

   //print the path to the console
   if(returnVal == JFileChooser.APPROVE_OPTION)
   {
     String path = chooser.getSelectedFile().getAbsolutePath();
     println("File path: " + path);
   }
 }
 
 void display()
 {
   click();
   
   stroke(0);
   strokeWeight(2);
   
   if(clicked)
   {
     fill(200);
   }
   else
   {
     fill(150);
   }
   
   rect(xpos, ypos, btnX, btnY);

   fill(0);
   textSize(15);
   text("Music", xpos + 3, ypos + btnY * .7);
 }
 
 //called to check if the user is clicking the button
 boolean click()
 {
   if(locked && !clicked)
   {
     return false;
   }
   
   if((overRect(xpos, ypos, btnX, btnY) && mousePressed)
     || clicked == true)
   {
     clicked = true;
     locked = true;
     return true;
   }
   else
   {
     return false;
   }
 }
}

//FUNCTION TAKEN FROM THE HANDLES EXAMPLE FROM PROCESSING.ORG
boolean overRect(float x, float y, float width, float height)
{
 if (mouseX >= x && mouseX <= x+width &&
   mouseY >= y && mouseY <= y+height)
 {
   return true;
 }
 else
 {
   return false;
 }
}
Re: Help with file chooser
Reply #3 - Apr 7th, 2009, 7:34pm
 
you sir, are my hero. that loop/noloop strategy did work, just my own ineptitude prevented me from seeing it initially. Thank you so much.
Page Index Toggle Pages: 1