Question

I am trying to do something very basic:

When saving a file, JFileChooser should have options about what format I would like to save my file, and if none is selected, then it should save as .txt as default.

An easy thing one would think. Here is what I've got so far: A fully working read and write class, which takes filepath and filename, and creates that file. A JFileChooser with File Filter is also ready, but I just can't figure out how to actually use the information, which filter the user did choose...

here is how the chooser looks atm.

   chooser = new MyFileChooser(path);   
    FileFilter txtType = new FileNameExtensionFilter("Text File (.txt)", "txt");
    FileFilter htmlType = new FileNameExtensionFilter("HTML File (.HTML)", "HTML");
    chooser.addChoosableFileFilter(txtType);
    chooser.addChoosableFileFilter(htmlType);
    chooser.setFileFilter(txtType);
    chooser.setSelectedFile(new File("Text.txt"));
    int back = chooser.showSaveDialog(null);

    if(back == JFileChooser.APPROVE_OPTION) { 
      path = chooser.getSelectedFile().getPath();
      FileExplorer toWrite= new FileExplorer(path);
      toWrite.writeFile();
    }

Like mentioned, there are two problems:

-how can I specify, what extension the file should have by using the information from the chosen filter.

-how can I set .txt as a default, if no extension is declared in the file name field and no filter is chosen?

Basically all I'm asking is to get the basic or expected behavior done.

Would be great if someone could help. thx Haeri

Was it helpful?

Solution

I am really disappointed with Stackoverflow... Oh well.. Here is the modified FileChooser. Hope I could help someone out. PS: I noticed, that it wouldn't be a good idea to pack the write function inside the Filechooser. So best thing to do is to get the name + extension with the .getFullName(); method and send the name to a write function.

import java.io.File;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class MyFileChooser extends JFileChooser {

public MyFileChooser(String path, String defName) {
    super(path);
    FileFilter txtType = new FileNameExtensionFilter("Text File (.txt)", "txt");
    FileFilter htmlType = new FileNameExtensionFilter("HTML File (.HTML)", "HTML");
    this.addChoosableFileFilter(txtType);
    this.addChoosableFileFilter(htmlType);
    this.setSelectedFile(new File(defName));
    Action details = this.getActionMap().get("viewTypeDetails");
    details.actionPerformed(null);
}

public String getFullName() {
    String[] temp;
    String extens, temp2, temp3;

    temp3 = this.getSelectedFile().getName();
    try {
        temp = this.getFileFilter().getDescription().split("[\\(||\\)]");

        if (temp3.endsWith(temp[1])) {
            return temp3;
        }else{
            return temp3 + temp[1];
        }
    }catch (Exception e){

        try {
            temp2 = this.getSelectedFile().getName();
            extens = temp2.substring(temp2.lastIndexOf('.')).trim();

            return temp3;
        }catch (Exception ee) {
            return temp3 + ".txt";
        }               
    }
}


public String getFolderPath() {
    String inputName = this.getSelectedFile().getName();
    String fullPath = this.getSelectedFile().getPath();
    String result = fullPath.replaceAll(""+ inputName + "$", "");

    return result;
}

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top