• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Getting correct filename with JFileChooser

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know how to make sure that the filename that someone enters into a JFileChooser ends with the right extension? In my application, I want people to be able to save a .jpg file. I can make the JFileChooser show only directories and jpg files, but when a user goes to type in a filename, if he doesn't type ".jpg" after it, it's saved without an extension.
I figured one way to do this would be to just change the name of the File object after the JFileChooser has closed. I thought there would be a setName method in the File object, but there's not -- just a getName method.
Any help would be greatly appreciated.
Thanks,
Chris
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the sun tutorial has a good example for just what u r looking for.
here is the link
 
Chris Funkhouser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roy --
That's where I started, and I didn't see the answer to my problem. Can you be more specific?
Thanks,
Chris
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its 1 am here so ill post the code tomorrow but basically it tell you the whole code there.
even has an image filter source code for u.
u need to subclass the Filter class and use it on the fileChooser method setFilter.
sun made u a class that does the job called imageFilter at the url i gave u.
if u still cant figure it out ill post the code here tomorrow.
 
Chris Funkhouser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see the ImageFilter class, and I'm using a similar filter to only list directories and jpg files. But that still doesn't put any restriction on what the user can input into the filename field. I have the filename field set to "graph.jpg", but it doesn't stop the user from entering "graph.garbage" or "graph" or anything else, and a Windows machine will save the file as that.
What I'm looking for is a way to make sure that no matter what the user puts into the filename field, their file will be saved with a .jpg extension.
Thanks,
Chris
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i c now.
the best i can think of is to add an actionlistener to the filechooser, and when the user hits the enter key or tries to save the file
(this i called via the approveSelection() method which invokes your actionlistener).
in your actionListener get the file and check the ending. if it is jpg then allow it, if not retrun back to the filechooser screen.
i think this will solve your problem.
 
Chris Funkhouser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Roy.
Here's the way I ended up handling it:
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
selectedFile = fileChooser.getSelectedFile();
if (selectedFile.getName().endsWith(".jpg")) {
//Code to create file
} //end if
else {
//Code to return error message to user
} //end if
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
glad u worked it out
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic