come salvi? Questo non vuole assolutamente essere un esempio di buona programmazione... ma il file viene salvato, e mantiene blu e grassetto.
codice:import java.awt.*; import java.io.*; import javax.swing.*; import java.awt.event.*; import javax.swing.text.*; import javax.swing.text.html.*; /** * * @author Andrea */ public class JTextPaneTest extends JFrame { private String text = "Nel mezzo del cammin di nostra vita\n" + "mi ritrovai per una selva oscura\n"+ "che la diritta via era smarrita"; private JTextPane textPane = new JTextPane(); private JButton save = new JButton("SAVE"); private String filePath = "C:/Users/Andrea/Desktop/savefile.doc"; private Document document; private class SaveAction implements ActionListener { public void actionPerformed (ActionEvent ae) { StyledDocument doc = (StyledDocument)textPane.getDocument(); HTMLEditorKit kit = new HTMLEditorKit(); BufferedOutputStream out; try { out = new BufferedOutputStream(new FileOutputStream(filePath)); kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength()); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } private Document initDoc() { document = new DefaultStyledDocument(); SimpleAttributeSet attrs = new SimpleAttributeSet(); attrs.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.BLUE); attrs.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE); try { document.insertString(0, text, attrs); } catch (Exception e) { return null; //e.printStackTrace(); } return document; } public JTextPaneTest() { super("Test di salvataggio"); this.setSize(400,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textPane.setDocument(this.initDoc()); this.getContentPane().add(new JScrollPane(textPane), BorderLayout.CENTER); this.getContentPane().add(save, BorderLayout.SOUTH); save.addActionListener(new SaveAction()); this.setVisible(true); } public static void main (String[] args) { new JTextPaneTest(); } }

Rispondi quotando