String filePath = chooser.getSelectedFile().getAbsoluteFile() + ".html";
I'm adding the extension hard coded and then saving to it. I would like my app to be portable across platforms so adding .html manually may make this a Windows only solution.
If I want to save the file in another format, text for example, how do I detect which format the user selected? FileNameExtensionFilter
can add filters to the dialog but how do I get the return value for file type selected? From this I'm still unclear how to retrieve user selected file type.
alt text http://img98.imageshack.us/img98/4904/savef.jpg
How can I find out which of two filters the user selected as the save format? HTML or JPEG, how do I retrieve this info from JFileChooser? It has something to do with JFileChooser.getFileFilter()
.
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");
}
}