next up previous
Next: Text fields and Password Up: Document handling Previous: Document handling

Document significant methods

These methods are used to retrieve, insert, and remove text strings from a document. The specified offset and length determine the location and size of the string being manipulated. All methods throw a BadLocationException if the location specified is invalid.

public abstract String getText( int offset, int length)

public abstract void getText( int offset, int length, Segment txt )

public abstract void insertString(int offset, String str,
AttributeSet a) 

public abstract void remove(int offs,
int len)

This public abstract int getLength() method returns an integer containing the number of characters held by the document.

Detecting and Reporting changes to a document

In a text editor you may wish to detect when the user has typed something, deleted something, copied and pasted text etc. You may then wish to call special methods that check a document or save it or whatever.

The Document class supports a DocumentListener which waits for changes to the document (insertion or removal of elements).

DocumentListeners can be added or removed with the following methods:

public abstract 
void addDocumentListener(DocumentListener listener) 
public abstract
void removeDocumentListener( DocumentListener listener )

Each of the methods in th following group manages the positions of important parts of the document. The start and end methods return the starting and ending positions of the document, while the createPosition() method allows you to create a specific position within the document which tracks as changes are made.

public abstract Position getStartPosition()
public abstract Position getEndPosition()
public abstract Position createPosition(int offs)

The following methods allow properties to be managed. It was previously mentioned that custom properties could be assigned to the document and its elements, and these methods implement this capability.

public abstract Object getProperty(Object key)
public abstract void putProperty(Object key, Object value)

following methods manage the root element(s) of the document.

public abstract Element[] getRootElements()
public abstract Element getDefaultRootElement()

The public abstract void render(Runnable r) method allows the model to be safely rendered, if the model supports asynchronous updates. The given renderer will be executed in a safe way, preventing changes while it is active.

next up previous
Next: Text fields and Password Up: Document handling Previous: Document handling
Dave Marshall