• Post Reply Bookmark Topic Watch Topic
  • New Topic

JFileChooser.FILES_ONLY  RSS feed

 
shelly kalra
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have posted a similar query on the same site but since i didnt get any reply, m again trying my luck.
excuse me if this is not allowed.
I m using JFileChooser, with filter added.I want to choose file but not directory for which i have added the code
jStoryFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
But the above code doesnt seem to help, since the dialog dispose off on clicking open, even if a directory is selected.
After searching for a while I know that many people have faced the same problem but could not figure out the alternative they adopted.
help,
Shelly.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the code that you used?
 
shelly kalra
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my code -
private String browseImageFile()
{
if(imageFilePath == null)
jImageFileChooser = new JFileChooser();
else
{
jImageFileChooser = new JFileChooser(imageFilePath);
}
jImageFileChooser.setAccessory(new ImagePreview(jImageFileChooser));
jImageFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
Vector ext = new Vector();
ext.add("gif");
ext.add("jpg");


Filter filter = new Filter(
ext, "Just Images(jpg/gif)");
jImageFileChooser.addChoosableFileFilter(filter);
jImageFileChooser.setDialogTitle("Add Image");
jImageFileChooser.setAcceptAllFileFilterUsed(false);
if (jImageFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
{
imageFilePath = new String();
int slashIndx = jImageFileChooser.getSelectedFile().toString().lastIndexOf(FileUtils.getSeperator());
imageFilePath = jImageFileChooser.getSelectedFile().toString().substring(0,slashIndx);
if(!jImageFileChooser.getSelectedFile().isFile())
{
return null;
}
else
return (jImageFileChooser.getSelectedFile()).toString();
}
else
return null;
}
now if u select a directory which has no gif/bmp files and click open, the chooser dialog dispose off.
I have added a check if(!jImageFileChooser.getSelectedFile().isFile()) that returns null for such a case...but what i want is that my dialog shouldnot dispose.
thanks,
Shelly.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!