Implements a very simple lightweight text editing component.
It lets the user edit a single line of text using the keyboard.
The only special character that it knows about is backspace; all other
characters are added to the text. Selections are not supported, so
there's only a simple caret indicating the insertion point.
The component also displays a component name above the editable
text line, and draws a black frame whose thickness indicates whether
the component has the focus.
The component can be initialized to enable or disable input
through input methods. Other than that, it doesn't do anything
to support input methods, so input method interaction (if any)
will occur in a separate composition window. However, the
component is designed to be easily extended with full input
method support. It distinguishes between "displayed text" and
"committed text" - here, they're the same, but in a subclass
that supports on-the-spot input, the displayed text would be the
combination of committed text and composed text. The component
also uses TextLayout to draw the text, so it can be easily
extended to handle input method highlights.
Returns the text that the user has entered and committed.
Since this component does not support on-the-spot input, there's no
composed text, so all text that has been entered is committed.
Returns:
an AttributedCharacterIterator for the text that the user has entered and committed
Returns a subrange of the text that the user has entered and committed.
Since this component does not support on-the-spot input, there's no
composed text, so all text that has been entered is committed.
Parameters:
beginIndex - the index of the first character of the subrange
endIndex - the index of the character following the subrange
Returns:
an AttributedCharacterIterator for a subrange of the text that the user has entered and committed
getCommittedTextLength
public int getCommittedTextLength()
Returns the length of the text that the user has entered and committed.
Since this component does not support on-the-spot input, there's no
composed text, so all text that has been entered is committed.
Returns:
the length of the text that the user has entered and committed
Returns the text that the user has entered.
As TextLayout requires a font to be defined for each character,
the default font is applied to the entire text.
A subclass that supports on-the-spot input must override this
method to include composed text.
Returns a text layout for the text that the user has entered.
This text layout is created from the text returned by getDisplayText.
The text layout is cached until invalidateTextLayout is called.
Returns:
a text layout for the text that the user has entered, or null
Returns a 0-width caret rectangle for the given text index.
It is calculated based on the text layout returned by getTextLayout,
so this method can be used for the entire displayed text.
Parameters:
caret - the text index for which to calculate a caret rectangle
Returns a text hit info indicating the current caret (insertion point).
This class always returns a caret at the end of the text that the user
has entered. Subclasses may return a different caret or null.
Returns:
the caret, or null
insertCharacter
public void insertCharacter(char c)
Inserts the given character at the end of the text.
Handles the key typed event. If the character is backspace,
the last character is removed from the text that the user
has entered. Otherwise, the character is appended to the text.
Then, the text is redrawn.
Java, Java 2D, and JDBC are trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries. Copyright 1993-1999 Sun Microsystems, Inc. 901 San Antonio Road Palo Alto, California, 94303, U.S.A. All Rights Reserved.