Reputation: 537
I'm using an JFileChooser to make a save function. But I want to save my files in a .rtf format how do I do this?
My code right now is:
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser saveFile = new JFileChooser();
int option = saveFile.showSaveDialog(null);
saveFile.setDialogTitle("Save the file...");
// saveFile.setFileFilter(new FileNameExtensionFilter("Text Files", "txt", "rtf"));
if (option == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(saveFile.getSelectedFile().getPath()));
writer.write(text);
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "Failed to save the file");
}
}
}//End of method
}//End of inner class
So I need to implement a code for saving the file into a .rtf
Upvotes: 0
Views: 1479
Reputation: 168825
Presuming (in the absence of an SSCCE) that the JEditorPane
is already set to text/rtf
and contains some formatted text, the easiest way to serialize it is:
editorPane.write( writer );
See JTextComponent.write(Writer)
for details.
Upvotes: 2