Reputation: 317
I am trying to save JEditorpane content, but it is not getting save. Where am I going wrong ?
Also I want the file to be saved as .xsl file . How can I achieve this ?
public class XSLTEditor {
@SuppressWarnings("unused")
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame f = new JFrame("XSLT Editor");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JFileChooser fileChooser = new JFileChooser("C:\\xyz");
JPanel gui = new JPanel(new BorderLayout());
final JEditorPane document = new JEditorPane();
JScrollPane scrollPane = new JScrollPane(document);
gui.add(scrollPane, BorderLayout.CENTER);
JButton save = new JButton("Save File");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String cmd = ((AbstractButton) ae.getSource()).getText();
try {
if (cmd.equals("Save")) {
FileWriter out = new FileWriter("C:\\Users\\Desktop\\JEditorPane.txt");
out.write(document.getText());
out.close();
}
} catch (Exception f) {
f.printStackTrace();
}
}
});
gui.add(save, BorderLayout.NORTH);
f.setContentPane(gui);
f.pack();
f.setSize(400,400);
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}
I am not including Open file chooser button function here in the code as I can successfully do that.
Upvotes: 0
Views: 22