I have a Jtable which uses custom renderer and editor, which use JTextPane as editor and renderer, which uses DefaultStyledDocument as the text model. The table contains hyperlinks, which are text with HTML.Attribute.HREF attribute. In order to save this styled text into database I need to convert the DefaultStyledDocument into XML and trying to do that throws this exception:
javax.swing.text.html.HTML$Attributeis not serializable as a key in anAttributeSet.
How can I fix this?
HTML.Attributeis notSerializable. For some reason Java developers decided not to add serialization support toHTML.Attributeas well asHTML.Tag. The most probable reason is thatHTMLDocumentwhich uses them is serialized into HTML text, and thus there’s no need to serialize Java objects directly.It’s easy to fix, really. Create your own attribute:
This is roughly the implementation of
HTML.Attributeclass, withSerializableinterface added.Use
LinkAttribute.HREFeverywhere you usedHTML.Attribute.HREF.