我正在序列化JTextPane的Document,以便将其样式化为数据库。我在JTextPane上附加了一个caretListener,我想知道序列化这个Document是否也会序列化caretListener。我之所以需要知道这一点,是因为自定义的caretListener类包含JComboBox,当我尝试序列化时,我会得到以下异常:
java.io.NotSerializableException: com.apple.laf.AquaComboBoxUI我怀疑如果文档包含caretListener,这就是出现此异常的原因。
下面是序列化它的代码:
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();然后我就把data保存在数据库里。
增编
以下是自定义插入符号侦听器:
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
...
}发布于 2012-11-29 17:55:32
Document通过Writer序列化,通过Reader反序列化。使用JTextPane的getEditorKit()和工具包的写/读方法。
https://stackoverflow.com/questions/13631150
复制相似问题