Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to determine which FileChooser ExtensionFilter has been selected

910968May 13 2012 — edited May 14 2012
Hi,

I'm using a JavaFX fileChooser.showSaveDialog with two extension filters (*.csv and *.xml). How can I determine which filter was selected when the end-user clicks the save button?

I am writing a program that allows the end-user to create an output file in CSV or XML format.

If the end-user enters a file name with no extension, I need a way to determine if I should create a CSV or XML file. I would also like to add the proper extension to the file name if none is specified by the end-user. I want to do this based on which filter the end-user has selected. I wrote a Java program 5 years ago and I was able to do this with the following instruction:

String extension = jFileChooser.getFileFilter().getDescription();

I can't find a similar instruction for JavFX.

My JavaFX Code:
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialDirectory(new File(options.getOutputDirectory()));
ExtensionFilter filter1 = new FileChooser.ExtensionFilter("Comma Delimited (*.csv)", "*.csv");
fileChooser.getExtensionFilters().add(filter1);
ExtensionFilter filter2 = new FileChooser.ExtensionFilter("XML Document (*.xml)", "*.xml");
fileChooser.getExtensionFilters().add(filter2);
File file = fileChooser.showSaveDialog(stage);
String filename = file.getAbsolutePath();
...How do I determine which extension filter has been selected?

My Java Code:
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setCurrentDirectory(new File(outputDirectory));
jFileChooser.setSelectedFile(new File(backupfile));
FileFilter filter = new FileNameExtensionFilter("Comma Delimited (*.csv)", "csv");
jFileChooser.addChoosableFileFilter(filter);
jFileChooser.setFileFilter(filter);
filter = new FileNameExtensionFilter("XML Document (*.xml)", "xml");
jFileChooser.addChoosableFileFilter(filter);
jFileChooser.setAcceptAllFileFilterUsed(false);
jFileChooser.setDialogTitle("Export Alarms");
jFileChooser.setBackground(colorFileChooser);

int returnVal = jFileChooser.showDialog(jFrame, "Export");

if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = jFileChooser.getSelectedFile();
    String filename = file.getAbsolutePath();
    String extension = jFileChooser.getFileFilter().getDescription();
    if (extension.equals("XML Document (*.xml)")) {
        if (!filename.endsWith(".xml") && !filename.endsWith(".XML")) {
            filename = filename + ".xml";
        }
        saveTableXML(filename);
    }
    else if (extension.equals("Comma Delimited (*.csv)")) {
        if (!filename.endsWith(".csv") && !filename.endsWith(".CSV")) {
            filename = filename + ".csv";
        }
        saveTableCSV(filename);
    }
}
Thanks,

Barry

Edited by: 907965 on May 13, 2012 1:14 PM

Edited by: 907965 on May 13, 2012 1:15 PM

Edited by: 907965 on May 13, 2012 1:19 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 11 2012
Added on May 13 2012
1 comment
2,925 views