tags:

views:

54

answers:

2

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?

+3  A: 

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

The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.

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 implements a locking mechanism for the document it allows multiple readers or one writer, and writers must wait until all observers of the document have been notified of a previous change before beginning another mutation to the document.

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

A document that can be marked up with character and paragraph styles in a manner similar to the Rich Text Format. The element structure for this document represents style crossings for style runs. These style runs are mapped into a paragraph element structure (which may reside in some other structure). The style runs break at paragraph boundaries since logical styles are assigned to paragraph boundaries.

DefaultStyledDocument allows you to place special characters within the document to help with formatting etc... Think Microsoft Word when you think about DefaultStyledDocument.

DocumentFilter

hen a Document containing a DocumentFilter is modified (either through insert or remove), it forwards the appropriate method invocation to the 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

A document that models HTML. The purpose of this model is to support both browsing and editing.

HTMLDocument should be used when you are creating/modifying documents that contain HTML code and are intended to be viewed in a browser.

Robert Greiner
+1  A: 

Your requirements aren't clear (to me at least). The key is how you want the document displayed.

JTextField is for a single line of text, so that is ruled out.

JTextArea is for multiple lines of text but does not support specific attributes. So you are limited to a single font and a single color.

JTextPane supports multiple lines of text and supports different types of attributes, like bold, italic and so on.

The Document gets more complicated the more features you require.

If this data is editable, then you will need to build in support to limit each Document to 3 lines of text. Again your definition of a line is not clear. That is can a line be 1k long or a maximum of 40 characters so it can be displayed on the screen as a single line without wrapping.

I would guess a JTextArea is what you need and I believe it uses a PlainDocument.

camickr
Ok i can see how its not clean. the 3 lines part was really just a bad way of saying how little text there's going to be. Whether there'll be a hard limit some place i haven't decided.As for PlainDocument it doesn't seem to have character level attributes so that wont do./
takoi