2

I'm serializing a JTextPane's Document in order to persist it's styled text to database. I have a caretListener attached to the JTextPane and I'm wondering if serializing this Document serializies the caretListener as well. The reason why I need to know this is because the custom caretListener class containes JComboBox and I get the following exception when I attempt the serialization:

java.io.NotSerializableException: com.apple.laf.AquaComboBoxUI

I suspect that if the Document contains the caretListener, that's the reason for this exception.

Here's the code that serializes it:

DefaultStyledDocument doc = (DefaultStyledDocument) getCellEditor().getCellEditorValue();
doc.setDocumentFilter(null);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject((DefaultStyledDocument) doc);
oos.flush();

byte[] data = bos.toByteArray();

oos.close();
bos.close();

And then I'm just saving data in the database.

Addendum

Here's the custom caret listener:

MyTextPane textpane = new MyTextPane();
textpane.addCaretListener(new caretListener());
public class caretListener implements CaretListener {

    MyTextpane textArea;
    JToggleButton boldbutton;
    JToggleButton italicbutton;
    JToggleButton underlinebutton;
    JComboBox fontscomboBox;
    JComboBox fontSizecombobox;
    // Methods
    ...
}
Igor
  • 1,532
  • 4
  • 23
  • 44
  • Why serializing the document and not just writing your own mechanism. See the warning at the end of each swing class: *Serialized objects of this class will not be compatible with future Swing releases* – Robin Nov 29 '12 at 17:30
  • @Robin Seemed like the easier thing to do. If it is the cause of the problem, I'll write my own mechanism, tags maybe. – Igor Nov 29 '12 at 17:40

1 Answers1

2

Document is serialized via Writer and deserialized via Reader. Use the JTextPane's getEditorKit() and write/read methods of the kit.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Thanks for the answer, but it doesn't really answer the main Question, which is in the title. It would be great if you could add that. – Igor Nov 29 '12 at 18:09
  • The problem arose [here](http://stackoverflow.com/q/13540613/230513), but I have _no_ idea how a `ComboBoxUI` gets dragged in. +1 for `EditorKit`. – trashgod Nov 29 '12 at 19:39
  • @trashgod Could it be because the caret listener has `JComboBox` fields. Please see the **addendum** in the question. – Igor Nov 29 '12 at 20:20
  • @StanislavL I just read that I can't save the alignment with `EditorKit`. – Igor Nov 29 '12 at 23:45
  • ...and I'm using `DefaultStyledDocument` instead of `RTFEditorDocument` – Igor Nov 30 '12 at 01:03
  • @Igor You can try this one http://java-sl.com/advanced_rtf_editor_kit.html it saves alignment, tables, images etc. – StanislavL Nov 30 '12 at 05:52
  • You should not serialize the listener. Add the listener just once to the JTextPane. Loading the new content won't change the listener of the pane. – StanislavL Nov 30 '12 at 05:55
  • @StanislavL Am I serializing the listener with the above code? I tried using the `AdvancedRTFEditorKit` and I get an exception saying `DefaultStyledDocument cannot be cast into AdvancedRTFEditorDocument`. How can I convert `AdvancedRTFEditorKit` into `DefaultStyledDocument`? – Igor Nov 30 '12 at 13:10
  • You don't need serializing listener. Listener is part of controller not model. You got the exception because wrong editor kit is used. – StanislavL Nov 30 '12 at 13:56
  • @StanislavL Actually that was a question :) _'Am I serializing the listener with the code above?'_ And obviously I have to use `AdvancedRTFEditorKit` so how can I avoid getting the exception? – Igor Nov 30 '12 at 17:07
  • My answer means you should not serialize it this way but instead write own Reader and Writer. See mentioned above example. – StanislavL Dec 02 '12 at 06:15
  • @StanislavL I'm not sure why, but your way of saving the `Document` works and the errors don't show up. Now I just have to figure out how to use `AdvancedRTFEditorKit` instead of `RTFEditorKit`in order to save the alignment as well. Thanks. – Igor Dec 03 '12 at 13:18