Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

java - JFileChooser(showSaveDialog) cant get the value of the extension file chosen

Im making a desktop application and it has a JFileChooser(ShowSaveDialog) function.. When I tried to save a sample text file the program didnt get the extension file that I chose.. I'm trying to use the if else or switch statement and I cant figure it out what command will I use to get the string/Int value for the condition if pdf,word or txt extension is chosen as file extension...

public class Save {
    static boolean flag = false;
    public static void main(String[] args) throws IOException, SQLException {
        JFileChooser saveFile = new JFileChooser();
        saveFile.setDialogTitle("Save as"); 

        FileNameExtensionFilter File_ext_txt =
            new FileNameExtensionFilter("Text Documents(*.txt)", "txt");
        FileNameExtensionFilter File_ext_pdf =
            new FileNameExtensionFilter("PDF", "pdf");
        FileNameExtensionFilter File_ext_doc =
            new FileNameExtensionFilter("Word 97-2003 Document", "doc");
        saveFile.addChoosableFileFilter(File_ext_pdf);
        saveFile.addChoosableFileFilter(File_ext_doc);
        saveFile.addChoosableFileFilter(File_ext_txt);

        FileFilter extension = saveFile.getFileFilter();
        int userSelection = saveFile.showSaveDialog(null);
        File File_Path = saveFile.getSelectedFile();
        String fullPath = File_Path.getAbsolutePath();
        String Ext = null;
        if (userSelection == JFileChooser.APPROVE_OPTION){
            if(extension == File_ext_txt){
                Ext = "txt";
            }

            File save = new File(fullPath+"."+Ext);
            System.out.println(extension);
            flag = save.createNewFile();
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've encountered this issue before. This is a utility function from one of my programs that you can use instead of JFileChooser.getSelectedFile, to get the extension too.

/**
 * Returns the selected file from a JFileChooser, including the extension from
 * the file filter.
 */
public static File getSelectedFileWithExtension(JFileChooser c) {
    File file = c.getSelectedFile();
    if (c.getFileFilter() instanceof FileNameExtensionFilter) {
        String[] exts = ((FileNameExtensionFilter)c.getFileFilter()).getExtensions();
        String nameLower = file.getName().toLowerCase();
        for (String ext : exts) { // check if it already has a valid extension
            if (nameLower.endsWith('.' + ext.toLowerCase())) {
                return file; // if yes, return as-is
            }
        }
        // if not, append the first extension from the selected filter
        file = new File(file.toString() + '.' + exts[0]);
    }
    return file;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...