Java Reference
In-Depth Information
The
getChange()
method of
DocumentEvent
requires an
Element
to return a
DocumentEvent.ElementChange
. You normally use the default root element from the
Document
, as in the following example.
Document documentSource = documentEvent.getDocument();
Element rootElement = documentSource.getDefaultRootElement();
DocumentEvent.ElementChange change = documentEvent.getChange(rootElement);
Once you have your
DocumentEvent.ElementChange
instance, you can find out the added
and removed elements, if you need that level of information.
public interface DocumentEvent.ElementChange {
public Element[ ] getChildrenAdded();
public Element[ ] getChildrenRemoved();
public Element getElement();
public int getIndex();
}
Caret and Highlighter Interfaces
Now that you understand the data model aspect of a text component, you can look at some
aspects of its selection rendering through the
Caret
and
Highlighter
interfaces. Remember
that these are properties of the text component, not the data model.
The
Caret
interface describes what's usually referred to as the
cursor
: the location in the
document where you can insert text. The
Highlighter
interface provides the basis for how to
paint selected text. These two interfaces, their related interfaces, and their implementations
are rarely altered. The text components simply use their default implementations with the
DefaultCaret
and
DefaultHighlighter
classes.
Although you probably won't alter the caret and highlighter behavior for a text component,
you should know that there are many interrelated classes working together. For the
Highlighter
interface, the predefined implementation is called
DefaultHighlighter
, which extends another
implementation called
LayeredHighlighter
. The
Highlighter
also manages a collection of
Highlighter.Highlight
objects to designate highlighted sections.
The
DefaultHighlighter
creates a
DefaultHighlighter.HighlightPainter
to paint
the highlighted section(s) of text. The
HighlightPainter
is an implementation of the
Highlighter.HighlightPainter
interface and extends the
LayeredHighlighter.LayerPainter
class. Each section to paint is described by a
Highlighter.Highlight
, where the
Highlighter
manages the set. The actual
HighlightPainter
is created by the
DefaultCaret
implementation.
The
Highlighter
interface describes how to paint selected text within a text component. If
you don't like the color, you can simply change the
TextField.selectionBackground
UI property
setting to a different color.
public interface Highlighter {
// Properties
public Highlighter.Highlight[ ] getHighlights();
// Other methods
public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter p)
throws BadLocationException;