I’ve been reading the javadocs trying to grasp around the swing Document API but I cant get something sensible out of it because there’s so many classes: Document, StyledDocument, AbstractDocument, DefaultStyledDocument, PlainDocument, HTMLDocument, and someone mentioned DocumentFilter. This question is more on a general basis so can someone give an overview of the differences between the implementations and when the different interfaces and abstracts are for?
For my specific case what I want to achieve is a data structure that will:
- hold three lines of text only. And
- attributes must not be per line or document. I will have a couple of thousand of these in some other structure so
- overhead is important.
Anything that i can use for this or is it better to extend something? If so, what?
All of the Document classes you list have the same base functionality and each expands based on a niche that needs to be filled. Really, it’s just a matter of realizing what you need to do and use the appropriate document type. For instance, if I am editing an HTML file, then I would use the HTMLDocument class.
I included a brief description of each of the Document classes you requested in your question below.
Document
This is the interface that all other Document types will inherit from. It provides the contract for all other Document types to follow.
AbstractDocument
This class allows you to work with different types of documents and uses a very lose ruleset. This class is more difficult to implement because it is so generic.
StyledDocument
Another interface that provides a contract for all styled documents. DefaultStyledDocument implements this interface, so we’ll get to that next.
DefaultStyledDocument
DefaultStyledDocument allows you to place special characters within the document to help with formatting etc… Think Microsoft Word when you think about DefaultStyledDocument.
DocumentFilter
This is an extremely useful class that “listens” for events to occur against your document (i.e. modification) and will perform an action when each event occurs.
PlainDocument
implements AbstractDocument and does not contain any kind of formatting special characters (Think notepad vs. Word). You should use this when you just want to store text (log file, etc.)
HTMLDocument
HTMLDocument should be used when you are creating/modifying documents that contain HTML code and are intended to be viewed in a browser.