Results 1 to 2 of 2
  1. #1
    JavaFan is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Extension problem using JFileChooser

    Hello,

    Can anyone please help me with this? I'm using a JFileChooser to create new files, I want it to save them in a ".txt" format, the problem is the user has to explicitely add ".txt" to his file name, otherwise it will save it as "what_the_user_has_typed" without any extension...

    and after I recover the filename the user has typed, there's no function to change the name as "setName"

    Java Code:
    File file = new File();
    file.setName(file.getName()+".txt");
    And the renameTo(new File()); fails, can I program it so it automatically adds the ".txt" extension to what the user has typed?

    Thank you :)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Extension problem using JFileChooser

    after I recover the filename the user has typed, there's no function to change the name as "setName"
    What about

    Java Code:
    file = new File(file.getParent(), newName);
    where file is as returned by the dialog, and newName is the name you would prefer to use.

    -----

    Strictly speaking ".txt" is an extension not a format; files are like roses in this respect.

    This isn't just a quibble. It's up to your program what format you save a file in, but you should be a bit careful about rejecting the name that the user wishes to give it. I don't appreciate the likes of Notepad being "helpful". Perhaps add a checkbox "add .txt" or something, and respect the filename if it has a . in it.

Similar Threads

  1. JFileChooser Problem
    By gkoef in forum New To Java
    Replies: 5
    Last Post: 04-29-2011, 03:08 PM
  2. Problem with saving from JFileChooser
    By fireshadow4126 in forum AWT / Swing
    Replies: 2
    Last Post: 11-24-2010, 09:21 PM
  3. Problem with JFileChooser and BufferedImage Class
    By toro1984 in forum AWT / Swing
    Replies: 3
    Last Post: 11-03-2010, 11:09 AM
  4. JFileChooser problem
    By jperson in forum New To Java
    Replies: 2
    Last Post: 02-06-2010, 08:31 PM
  5. jfilechooser and jtextarea problem
    By kostinio in forum AWT / Swing
    Replies: 7
    Last Post: 10-07-2009, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •