GeeCON Prague 2014*
The moose likes Java in General and the fly likes Getting correct filename with JFileChooser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting correct filename with JFileChooser" Watch "Getting correct filename with JFileChooser" New topic
Author

Getting correct filename with JFileChooser

Chris Funkhouser
Greenhorn

Joined: Dec 10, 2001
Posts: 12
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
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
the sun tutorial has a good example for just what u r looking for.
here is the link
Chris Funkhouser
Greenhorn

Joined: Dec 10, 2001
Posts: 12
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

Joined: Jan 13, 2002
Posts: 732
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

Joined: Dec 10, 2001
Posts: 12
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

Joined: Jan 13, 2002
Posts: 732
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

Joined: Dec 10, 2001
Posts: 12
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

Joined: Jan 13, 2002
Posts: 732
glad u worked it out
 
Don't get me started about those stupid light bulbs.
 
subject: Getting correct filename with JFileChooser