Java Reference
In-Depth Information
The data model is stored apart from the text component (the view). Therefore, if you're
interested in monitoring changes to the content of a text component, you must watch the
Document itself, not the text component, for changes. If the changes reach the text component,
it's too late—the model has already changed. To listen for changes, attach a DocumentListener
to the model. However, the more likely scenario for restricting input is to provide a custom
model or attach a DocumentFilter to the AbstractDocument . You can also attach an InputVerifier
to the text component. However, that wouldn't be used until the input focus tried to leave the
component.
Note In addition to accessing the textual content through the Document interface, a framework is defined
to support undo/redo capabilities. This will be explored in Chapter 21.
Now, let's look at the pieces that make up a Document . First, here's the base interface definition:
public interface Document {
// Constants
public final static String StreamDescriptionProperty;
public final static String TitleProperty;
// Listeners
public void addDocumentListener(DocumentListener listener);
public void removeDocumentListener(DocumentListener listener);
public void addUndoableEditListener(UndoableEditListener listener);
public void removeUndoableEditListener(UndoableEditListener listener);
// Properties
public Element getDefaultRootElement();
public Position getEndPosition();
public int getLength();
public Element[ ] getRootElements();
public Position getStartPosition();
// Other methods
public Position createPosition(int offset) throws BadLocationException;
public Object getProperty(Object key);
public String getText(int offset, int length) throws BadLocationException;
public void getText(int offset, int length, Segment txt)
throws BadLocationException;
public void insertString(int offset, String str, AttributeSet a)
throws BadLocationException;
public void putProperty(Object key, Object value);
public void remove(int offset, int len) throws BadLocationException;
public void render(Runnable r);
}
The content within a Document is described by a series of elements in which each element
implements the Element interface. Within each element, you can store attributes so that select
 
Search WWH ::




Custom Search