Domanda

I already have a method which export data as a csv. No I need to implement an export as a xml and as a java object itself.
So I need to know, as what the user will save it. I want to find out what extention he insert in the already made JFileChooser.

Here's the code I already have:

JFileChooser chooser = new JFileChooser();
int opt = chooser.showSaveDialog(frame);
if (opt != JFileChooser.APPROVE_OPTION)
    return;
try {
    File file = chooser.getSelectedFile();
    export(file);
    JOptionPane.showMessageDialog(frame, file,
            getText("message.filesaved"),
            JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
    JOptionPane.showMessageDialog(frame, e.getMessage(),
            getText("message.error"), JOptionPane.ERROR_MESSAGE);
}

So how can I extend this method to allow the user, to choose if he will export it as a csv (already done), xml or java object?

What I already tried:
I tried to use the FileNameExtensionFilter to add a CSV option, a XML option or a Java Object option. But with this method, I can't find out which one the user took.

È stato utile?

Soluzione

Since you already added FileFilters, you can simply use the getFileFilter() method.

http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getFileFilter%28%29

It will return the selected FileFilter and all you have to do is to append the extention to the file name.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top