• 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:

JFileChoosers and file extensions

 
Greenhorn
Posts: 4
Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys, I've been testing things for a while since i'm learning java; I'm doing a playlist editor/creator and most of the problems I have are due to swing, and here comes my question. I used JFileChooser to open files without any problem, the FileNameExtensionFilters worked well, but now i'm a bit disoriented about what to do when saving with different file extensions, here's an example code:



This isn't working, it saves files without any extension unless I explicitly include the extension in the name when I select the file to save
I can already tell that the FileNameExtensionFilters only work when selecting files to be opened, but I still haven't figured out yet how to make extensions work. How can I force file extensions when saving according to the selected FileNameExtensionFilter? Any leads on this would be helpful, thanks in advance
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JFileChooser gives you a selectedFile. Remember that a java.io.File isn't a computer file in the usual sense; the API makes it clear that it is (emphasis added)

An abstract representation of file and directory pathnames.


What you do with the pathname before you use it to create a physical file on disk is up to you. .

Most programs resort to appending a suitable extension if the user-selected file is not valid. You can easily do the same, using JFileChooser#getFileFilter() to get a reference to the selected filter; call its accept(File f) method to determine whether the file name is acceptable; if not, cast the reference and call FileNameExtensionFilter#getExtensions()[0] to get the desired extension.
 
Marshal
Posts: 80735
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
larry clay
Greenhorn
Posts: 4
Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help and welcome guys!

It works now and this is the corrected snippet. This only works with single extension filefilters since I only do a getExtensions()[0] method call. Filters with multiple extensions won't allow me to know which extension is the chosen one, neither the chosen File path if I don't explicitly name it with the desired extension so I can't do it this way ... I can't come with a workaround to this but neither do I need to implement it now. At least I know a little more about this matter now

 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

larry clay wrote:This only works with single extension filefilters since I only do a getExtensions()[0] method call. Filters with multiple extensions won't allow me to know which extension is the chosen one


Use a separate filter for each extension and like I already said, use getFileFilter() to get the selected filter.
reply
    Bookmark Topic Watch Topic
  • New Topic