JFileChooser with extensions

Mickeys
Mickeys used Ask the Experts™
on
I am trying to just be able to save and open files with the extenstion .rov.

I have done like the code bellow.
In the open is it possible to not be able to choose "All files" so only .rov will be in the choosing list?

In the save list....the .rov is showing but when saving it dont save with that extenstion. Why?
....main class
-----------------
 else if(choice.equals("Open"))
            {
                JFileChooser fc = new JFileChooser();
                fc.addChoosableFileFilter(new RovFilter());
                int returnVal = fc.showOpenDialog(null);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();

                FileReader fr = new FileReader(file);                
                BufferedReader br = new BufferedReader(fr);
                FwReader rr = new FwReader(br);

                output.setText(rr.nextRomanAsInt());
                }
            }

            else if(choice.equals("Save"))
            {
                JFileChooser fc = new JFileChooser();
                fc.addChoosableFileFilter(new RovFilter());
                int returnVal = fc.showSaveDialog(null);

                if (returnVal == JFileChooser.APPROVE_OPTION)
                {
                   File file = fc.getSelectedFile();
                  
                   FileWriter fw = new FileWriter(file);
                   BufferedWriter bf = new BufferedWriter(fw);                  
                   FwWriter fwW = new FwWriter(bf);
                   fwW.write(output.getText());
                   fwW.close();                   
                }
            }


-------------------
util
-------------------
public class Utils {

    public final static String rov = "rov";

    /*
     * Get the extension of a file.
     */
    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
}


-------------------
Filter
-------------------
import java.io.File;
import javax.swing.filechooser.*;


public class RovFilter extends FileFilter {

   
    public boolean accept(File f) {
        if (f.isDirectory()) {
            return true;
        }

        String extension = Utils.getExtension(f);
        if (extension != null) {
            if (extension.equals(Utils.rov)) {
                    return true;
            } else {
                return false;
            }
        }

        return false;
    }

   
    public String getDescription() {
        return ".rov";
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:

>> In the open is it possible to not be able to choose "All files" so only .rov will be in the choosing list?

>> JFileChooser fc = new JFileChooser();
>> fc.addChoosableFileFilter(new RovFilter());

After the above lines.. You need to do..
fc.setAcceptAllFileFilterUsed(false);

Commented:
To get the extension..

You need to use the fc.getFileFilter( ). getDescription( )

getSelectedFile( ) does not returns the extension
Top Expert 2016

Commented:
>>In the open is it possible to not be able to choose "All files" so only .rov will be in the choosing list?

Should be automatic if your FileFilter is right - please post it
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Top Expert 2016

Commented:
>>Should be automatic if your FileFilter is right - please post it

(Same goes for saving the file too)

Author

Commented:
I have posted my filter in my main window.

Author

Commented:
o I have only got help with one thing and that is in the save window that is is only possible to save .rov.

What I still miss is that when I save my file I want it to add so it saves with ****.rov. No it only saves ***** and no extension. My gues is that it should be here:

File file = fc.getSelectedFile();   (but how do I add the .rov to my file?

Commented:
>> but how do I add the .rov to my file?

File file = fc.getSelectedFile();
file = new File(file.getAbsolutePath( ) + fc.getFileFilter( ). getDescription( ));

then.. file will refer to the file with ".rov" extension..

Your file filter description will return the ".rov"
Top Expert 2016

Commented:
>> but how do I add the .rov to my file?

Just add the extension. Doing that via getDescription would only work if the description only consisted of the extension, which wouldn't be much of a description:

File file = fc.getSelectedFile();
file = new File(file.getAbsolutePath( ) + ".rov";

Open in new window

Top Expert 2016

Commented:
(Of course, you should probably check to see if .rov is *already* the extension)
Top Expert 2016

Commented:
>>I have posted my filter in my main window.

Unfortunately it depends on code that is not here
Commented:
>> Doing that via getDescription would only work if the description onlyconsisted of the extension, which wouldn't be much of a description:

getDescription( ) api of Filter Class in the code that is posted in the question..

According to that.. you can getDescription( )


public String getDescription() {
        return ".rov";
    }

Open in new window

Top Expert 2016

Commented:
>>According to that.. you can getDescription( )

Yes - i didn't see that as i'm not getting any scrollbar on the snippet window

Yes, that certainly isn't "much of a description". It would probably be better to use the 'Utils.rov' thing

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial