javasavejfilechooser

How do I assign a file extension to a file I'm trying to save using JFileChooser?


I got the loading aspect done, but I am having problems figuring out how to get the files to save with the proper extensions, in this case it would be .png or .jpg. Any suggestions?

I want it to save automatically with .jpg, .png or etc. and currently its making it so that the user has to type these extensions at the end of the file which is the opposite of what I want.


Solution

  • You need to modify the object like this:

    JFileChooser c = new JFileChooser();
    File f = c.getSelectedFile();
    String filePath = f.getPath();
    if(!filePath.toLowerCase().endsWith(".jpg"))
    {
        f = new File(filePath + ".jpg");
    }