Java JFileChooser getAbsoluteFile Add File Extension

11,871

Solution 1

Here is the code snippet that solves the issue:

JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
chooser.setAcceptAllFileFilterUsed(false);

FileNameExtensionFilter filter = new FileNameExtensionFilter("HTML Documents", "htm", "html");
chooser.setFileFilter(filter);

int option = chooser.showSaveDialog(ChatGUI.this);
if (option == JFileChooser.APPROVE_OPTION) {
    // Set up document to be parsed as HTML
    StyledDocument doc = (StyledDocument)textPaneHistory.getDocument();
    HTMLEditorKit kit = new HTMLEditorKit();

    BufferedOutputStream out;

    try {
        System.out.println(chooser.getFileFilter());

        if (chooser.getFileFilter() == filter)
            System.out.println("ha ha");
    }
}

Solution 2

You're probably looking for this:

The trick consists in casting the returned FileFilter to FileNameExtensionFilter and then apply getExtensions().

JFileChooser fileChooser = new JFileChooser("");

// Prevent user to use the default All Files option
fileChooser.setAcceptAllFileFilterUsed(false);

[...]

// Get the FileFilter
FileFilter ff = fileChooser.getFileFilter();

// Cast the FileFilter to FileNameExtensionFilter
FileNameExtensionFilter extFilter = (FileNameExtensionFilter)ff;

// Get the Extension
String ext = extFilter.getExtensions()[0];

Or, for making it compact:

ext = ((FileNameExtensionFilter)fileChooser.getFileFilter()).getExtensions()[0];
Share:
11,871
iTEgg
Author by

iTEgg

:)

Updated on June 28, 2022

Comments

  • iTEgg
    iTEgg about 2 years

    i have this issue working but i would like to know if there is a better way of adding the file extension?

    what i am doing right now is:

    String filePath = chooser.getSelectedFile().getAbsoluteFile() + ".html";
    

    im adding the extension hard coded. and then saving to it.

    just wondering if there is a more robust/logical manner this can be implemented?

    thank you for your time.

    EDIT: i ask this as i would like my app to be portable across platforms. so adding .html manually i may make this a windows only solution.

    EDIT: i think ive surfed enough to know that .html hard coded is safe as i havent found any documentation that says dont take this approach (not completely sure).

    ISSUE: also if i want to save the file in another format, text, for example how do i detect that the user selected which format?

    FileNameExtensionFilter can add filters to the dialog but how do i get the return value for file type selected?

    EDIT: i have studied this but still unclear how to retrive user selected file type.

    EDIT: this is a rephrase of my issue:

    alt text http://img98.imageshack.us/img98/4904/savef.jpg my question is how can i retrieve/find out which one of the two filters the user has selected as the save format. HTML or JPEG? how do i retrieve this info from JFileChooser? thank you.

    EDIT: found something out: it has something to do with JFileChooser.getFileFilter() your help still welcome.

    EDIT: getFileFilter() and FileNameExtensionFilter comparasion solved this issue.