Problem with SaveDialog & File extensions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Problem with SaveDialog & File extensions

  1. #1
    Join Date
    Oct 2003
    Posts
    8

    Problem with SaveDialog & File extensions

    I'm using JFileChooser class and I'm invoking the showSaveDialog method with a file filter on .txt files.
    Here is the problem:

    If I call the file in the dialog with the name of "test", I wish it creates a "test.txt" file (I've set the FileFilter for this reason), but it creates a file without extension called "test".
    What is the purpuose of the FileFilter in the SaveDialogs, then?

    Thanks
    R.B.

  2. #2
    Join Date
    Mar 2003
    Posts
    834

    Post

    From Sun's tutorial:

    "By default, a file chooser displays all of the files and directories that it detects, except hidden files. A program can apply one or more file filters to a file chooser so that the chooser shows only some files. "

    http://java.sun.com/docs/books/tutor...lechooser.html
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Oct 2003
    Posts
    8

    SaveDialog problems

    I understand the usefullness of FileFilter in OpenDialog. But I think there are some problems when I try to use the FileFilter in SaveDialog. I wish the user hasn't to write the extension of the file he want to create. I wish the extension is fixed in someway, so I thought to use the FileFilter, but it seems not to work.
    R.B.

  4. #4
    Join Date
    Mar 2003
    Posts
    834

    Thumbs down

    I think you'll have to write that bit yourself. Fortunately, it's not that difficult.
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Oct 2003
    Posts
    8

    SaveDialog problems

    I'll do so.
    R.B.

  6. #6
    Join Date
    Nov 2002
    Posts
    138
    here's a tip:

    from that part where you choose your file's savename, just put a .txt as an extension, everytime.

    so savename + .txt = you got it! of course, this is assuming that you only wish to save in .txt file extensions.

    now in order to make it more dynamic, if you're gonna be using a lot of filters, your app should know which filter was chosen. then from there, just add the appropriate extension. it has to "listen" to the filter selection. i don't know if java has a method for this by default, but it's certainly doable if it doesn't.

    as archangel put it, "...it's not that difficult."

  7. #7
    Join Date
    Mar 2003
    Posts
    834

    Thumbs down

    "so savename + .txt = you got it! of course, this is assuming that you only wish to save in .txt file extensions. "

    Except if someone enters the filename "Hello.txt" as it will become "Hello.txt.txt".
    ArchAngel.
    O:-)

  8. #8
    Join Date
    Oct 2003
    Posts
    8

    MyJFileChooser class

    To solve my little problem I've created a MyJFileChooser class which extends the JFileChooser class. I've added the solution of my problem: now the FileFileter associate to the JFileChooser fixs the extensions of files created with a SaveDialog. Well, these are my intensions, but the truth is bit different.

    The problem is this:
    When the construstor of MyJFileChooser class is invoked there is a Null Pointer exeption caused (I suppose) by the fact that JFileChooser in his constructor makes use of some protected methods (e.g setup(FileSystemView view)) that can't be extended.

    So how can I do If I wanted to add a particular new feature to a preexistent class like JFileChooser?
    R.B.

  9. #9
    Join Date
    Mar 2003
    Posts
    834
    Please post some code and your error message.

    Also, you should be calling getFileFilter() which will return the currently selected filter.
    ArchAngel.
    O:-)

  10. #10
    Join Date
    Nov 2002
    Posts
    138
    of course that's what a win Notepad would do too if the filter is set to .txt. so it doesn't matter, text editors will still read the file, and your OS can still associate it with a program to open it with. but then that's dumb.

    there you go, getFileFilter() should do it!

  11. #11
    Join Date
    Oct 2003
    Posts
    8

    Here is my code

    Here is the code:
    Code:
    import java.io.File;
    import javax.swing.*;
    
    public class MyJFileChooser extends JFileChooser
    {
    
      public MyJFileChooser()
      {
        super();
      }
    
      public File getSelectedFile() {
            File file = super.getSelectedFile();
    
            // Controllo che non sia giÓ stata messa a mano l'estensione
            // se l'estensione corrisponde a quella selezionata con il filtro
            // non aggiungo nulla, altrimenti aggiungo l'estensione
            
            // I check user hasn't just specified the right exetension for the file.
            // If It not so, I add it at the end of the pathname
            String pathname = file.getAbsolutePath();
            String extension = this.getFileFilter().getDescription();
            if(!(pathname.endsWith(extension)))
              pathname = pathname + extension;
    
            File fileOut = new File(pathname);
            return fileOut;
        }
    }
    When I try to use the constructor:
    Code:
    MyJFileChooser myFiler = new MyJFileChooser();
    I obtain a java.lang.NullPointerException

    [ArchAngel added CODE tags]
    R.B.

  12. #12
    Join Date
    Oct 2003
    Posts
    8

    Solution to my SaveDialog problems

    I've found the solution to my problem. The null-pointer exception was in my new overriden method getSelectedFile(), because I've not considered that the file read with super().getSelectedFile could be null.

    Here is how I solved the problem in the code:
    Code:
    .
    .
    .
    public File getSelectedFile() {
            File file = super.getSelectedFile();
    
            // Controllo che non sia giÓ stata messa a mano l'estensione
            // se l'estensione corrisponde a quella selezionata con il filtro
            // non aggiungo nulla, altrimenti aggiungo l'estensione
    
            // I check user hasn't just specified the right exetension for the file.
            // If It not so, I add it at the end of the pathname
            if(file != null)
            {
              String pathname = file.getAbsolutePath();
              String extension = this.getFileFilter().getDescription();
              if(!(pathname.endsWith(extension)))
                pathname = pathname + extension;
    
              File fileOut = new File(pathname);
              return fileOut;
            }
            else
              return null;
        }
    .
    .
    .
    And that's all.

    [ArchAngel added CODE tags]
    R.B.

  13. #13
    Join Date
    Mar 2003
    Posts
    834

    Thumbs up

    Thanks for posting your solution.
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center